2 * Functions to use the XRender extension
4 * Copyright 2001, 2002 Huw D M Davies for CodeWeavers
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
24 #include "wine/port.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
48 #include <X11/extensions/Xrender.h>
50 static XRenderPictFormat *screen_format; /* format of screen */
51 static XRenderPictFormat *mono_format; /* format of mono bitmap */
56 XFORM xform; /* this is dum as we don't care about offsets */
60 #define INITIAL_REALIZED_BUF_SIZE 128
62 typedef enum { AA_None, AA_Grey, AA_RGB, AA_BGR, AA_VRGB, AA_VBGR } AA_Type;
69 XRenderPictFormat *font_format;
84 COLORREF lastTextColor;
88 static gsCacheEntry *glyphsetCache = NULL;
89 static DWORD glyphsetCacheSize = 0;
90 static INT lastfree = -1;
93 #define INIT_CACHE_SIZE 10
95 static int antialias = 1;
97 /* some default values just in case */
99 #define SONAME_LIBX11 "libX11.so"
101 #ifndef SONAME_LIBXEXT
102 #define SONAME_LIBXEXT "libXext.so"
104 #ifndef SONAME_LIBXRENDER
105 #define SONAME_LIBXRENDER "libXrender.so"
108 static void *xrender_handle;
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)
126 static CRITICAL_SECTION xrender_cs;
127 static CRITICAL_SECTION_DEBUG critsect_debug =
130 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
131 0, 0, { 0, (DWORD)(__FILE__ ": xrender_cs") }
133 static CRITICAL_SECTION xrender_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
136 /***********************************************************************
137 * X11DRV_XRender_Init
139 * Let's see if our XServer has the extension available
142 void X11DRV_XRender_Init(void)
144 int error_base, event_base, i;
145 XRenderPictFormat pf;
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)))
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)
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 */
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;
183 pf.type = PictTypeDirect;
186 pf.direct.alphaMask = 1;
187 mono_format = pXRenderFindFormat(gdi_display, PictFormatType |
188 PictFormatDepth | PictFormatAlpha |
189 PictFormatAlphaMask, &pf, 0);
191 ERR("mono_format == NULL?\n");
192 X11DRV_XRender_Installed = FALSE;
199 if(X11DRV_XRender_Installed || client_side_with_core)
201 glyphsetCache = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
202 sizeof(*glyphsetCache) * INIT_CACHE_SIZE);
204 glyphsetCacheSize = INIT_CACHE_SIZE;
206 for(i = 0; i < INIT_CACHE_SIZE; i++) {
207 glyphsetCache[i].next = i + 1;
208 glyphsetCache[i].count = -1;
210 glyphsetCache[i-1].next = -1;
211 using_client_side_fonts = 1;
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)
218 if(screen_depth <= 8 || !client_side_antialias_with_render)
222 else TRACE("Using X11 core fonts\n");
225 static BOOL fontcmp(LFANDSIZE *p1, LFANDSIZE *p2)
227 if(p1->hash != p2->hash) return TRUE;
228 if(memcmp(&p1->xform, &p2->xform, sizeof(p1->xform))) return TRUE;
229 if(memcmp(&p1->lf, &p2->lf, offsetof(LOGFONTW, lfFaceName))) return TRUE;
230 return strcmpW(p1->lf.lfFaceName, p2->lf.lfFaceName);
234 static void walk_cache(void)
238 EnterCriticalSection(&xrender_cs);
239 for(i=mru; i >= 0; i = glyphsetCache[i].next)
240 TRACE("item %d\n", i);
241 LeaveCriticalSection(&xrender_cs);
245 static int LookupEntry(LFANDSIZE *plfsz)
249 for(i = mru; i >= 0; i = glyphsetCache[i].next) {
251 if(glyphsetCache[i].count == -1) { /* reached free list so stop */
256 if(!fontcmp(&glyphsetCache[i].lfsz, plfsz)) {
257 glyphsetCache[i].count++;
259 glyphsetCache[prev_i].next = glyphsetCache[i].next;
260 glyphsetCache[i].next = mru;
263 TRACE("found font in cache %d\n", i);
268 TRACE("font not in cache\n");
272 static void FreeEntry(int entry)
276 if(glyphsetCache[entry].glyphset) {
278 pXRenderFreeGlyphSet(gdi_display, glyphsetCache[entry].glyphset);
280 glyphsetCache[entry].glyphset = 0;
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;
294 glyphsetCache[entry].nrealized = 0;
298 static int AllocEntry(void)
300 int best = -1, prev_best = -1, i, prev_i = -1;
303 assert(glyphsetCache[lastfree].count == -1);
304 glyphsetCache[lastfree].count = 1;
306 lastfree = glyphsetCache[lastfree].next;
308 glyphsetCache[best].next = mru;
311 TRACE("empty space at %d, next lastfree = %d\n", mru, lastfree);
315 for(i = mru; i >= 0; i = glyphsetCache[i].next) {
316 if(glyphsetCache[i].count == 0) {
324 TRACE("freeing unused glyphset at cache %d\n", best);
326 glyphsetCache[best].count = 1;
328 glyphsetCache[prev_best].next = glyphsetCache[best].next;
329 glyphsetCache[best].next = mru;
337 TRACE("Growing cache\n");
340 glyphsetCache = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
342 (glyphsetCacheSize + INIT_CACHE_SIZE)
343 * sizeof(*glyphsetCache));
345 glyphsetCache = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
346 (glyphsetCacheSize + INIT_CACHE_SIZE)
347 * sizeof(*glyphsetCache));
349 for(best = i = glyphsetCacheSize; i < glyphsetCacheSize + INIT_CACHE_SIZE;
351 glyphsetCache[i].next = i + 1;
352 glyphsetCache[i].count = -1;
354 glyphsetCache[i-1].next = -1;
355 glyphsetCacheSize += INIT_CACHE_SIZE;
357 lastfree = glyphsetCache[best].next;
358 glyphsetCache[best].count = 1;
359 glyphsetCache[best].next = mru;
361 TRACE("new free cache slot at %d\n", mru);
365 static int GetCacheEntry(LFANDSIZE *plfsz)
367 XRenderPictFormat pf;
371 if((ret = LookupEntry(plfsz)) != -1) return ret;
374 entry = glyphsetCache + ret;
375 entry->lfsz = *plfsz;
376 assert(entry->nrealized == 0);
383 if(X11DRV_XRender_Installed) {
387 pf.direct.alphaMask = 0xff;
391 ERR("aa = %d - not implemented\n", entry->aa);
394 pf.direct.alphaMask = 1;
398 pf.type = PictTypeDirect;
402 entry->font_format = pXRenderFindFormat(gdi_display,
409 entry->glyphset = pXRenderCreateGlyphSet(gdi_display, entry->font_format);
415 static void dec_ref_cache(int index)
418 TRACE("dec'ing entry %d to %d\n", index, glyphsetCache[index].count - 1);
419 assert(glyphsetCache[index].count > 0);
420 glyphsetCache[index].count--;
423 static void lfsz_calc_hash(LFANDSIZE *plfsz)
425 DWORD hash = 0, *ptr;
428 for(ptr = (DWORD*)&plfsz->xform; ptr < (DWORD*)(&plfsz->xform + 1); ptr++)
430 for(i = 0, ptr = (DWORD*)&plfsz->lf; i < 7; i++, ptr++)
432 for(i = 0, ptr = (DWORD*)&plfsz->lf.lfFaceName; i < LF_FACESIZE/2; i++, ptr++) {
433 WCHAR *pwc = (WCHAR *)ptr;
443 /***********************************************************************
444 * X11DRV_XRender_Finalize
446 void X11DRV_XRender_Finalize(void)
450 EnterCriticalSection(&xrender_cs);
451 for(i = mru; i >= 0; i = glyphsetCache[i].next)
453 LeaveCriticalSection(&xrender_cs);
457 /***********************************************************************
458 * X11DRV_XRender_SelectFont
460 BOOL X11DRV_XRender_SelectFont(X11DRV_PDEVICE *physDev, HFONT hfont)
464 GetObjectW(hfont, sizeof(lfsz.lf), &lfsz.lf);
465 TRACE("h=%ld w=%ld weight=%ld it=%d charset=%d name=%s\n",
466 lfsz.lf.lfHeight, lfsz.lf.lfWidth, lfsz.lf.lfWeight,
467 lfsz.lf.lfItalic, lfsz.lf.lfCharSet, debugstr_w(lfsz.lf.lfFaceName));
468 lfsz.xform = physDev->dc->xformWorld2Vport;
469 lfsz_calc_hash(&lfsz);
471 EnterCriticalSection(&xrender_cs);
472 if(!physDev->xrender) {
473 physDev->xrender = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
474 sizeof(*physDev->xrender));
475 physDev->xrender->cache_index = -1;
477 else if(physDev->xrender->cache_index != -1)
478 dec_ref_cache(physDev->xrender->cache_index);
479 physDev->xrender->cache_index = GetCacheEntry(&lfsz);
480 LeaveCriticalSection(&xrender_cs);
484 /***********************************************************************
485 * X11DRV_XRender_DeleteDC
487 void X11DRV_XRender_DeleteDC(X11DRV_PDEVICE *physDev)
490 if(physDev->xrender->tile_pict)
491 pXRenderFreePicture(gdi_display, physDev->xrender->tile_pict);
493 if(physDev->xrender->tile_xpm)
494 XFreePixmap(gdi_display, physDev->xrender->tile_xpm);
496 if(physDev->xrender->pict) {
497 TRACE("freeing pict = %lx dc = %p\n", physDev->xrender->pict, physDev->hdc);
498 pXRenderFreePicture(gdi_display, physDev->xrender->pict);
502 EnterCriticalSection(&xrender_cs);
503 if(physDev->xrender->cache_index != -1)
504 dec_ref_cache(physDev->xrender->cache_index);
505 LeaveCriticalSection(&xrender_cs);
507 HeapFree(GetProcessHeap(), 0, physDev->xrender);
508 physDev->xrender = NULL;
512 /***********************************************************************
513 * X11DRV_XRender_UpdateDrawable
515 * This gets called from X11DRV_SetDrawable and X11DRV_SelectBitmap.
516 * It deletes the pict when the drawable changes.
518 void X11DRV_XRender_UpdateDrawable(X11DRV_PDEVICE *physDev)
520 if(physDev->xrender->pict) {
521 TRACE("freeing pict %08lx from dc %p drawable %08lx\n", physDev->xrender->pict,
522 physDev->hdc, physDev->drawable);
525 pXRenderFreePicture(gdi_display, physDev->xrender->pict);
528 physDev->xrender->pict = 0;
532 /************************************************************************
535 * Helper to ExtTextOut. Must be called inside xrender_cs
537 static BOOL UploadGlyph(X11DRV_PDEVICE *physDev, int glyph)
544 gsCacheEntry *entry = glyphsetCache + physDev->xrender->cache_index;
545 UINT ggo_format = GGO_GLYPH_INDEX;
547 if(entry->nrealized <= glyph) {
548 entry->nrealized = (glyph / 128 + 1) * 128;
551 entry->realized = HeapReAlloc(GetProcessHeap(),
554 entry->nrealized * sizeof(BOOL));
556 entry->realized = HeapAlloc(GetProcessHeap(),
558 entry->nrealized * sizeof(BOOL));
560 if(entry->glyphset == 0) {
562 entry->bitmaps = HeapReAlloc(GetProcessHeap(),
565 entry->nrealized * sizeof(entry->bitmaps[0]));
567 entry->bitmaps = HeapAlloc(GetProcessHeap(),
569 entry->nrealized * sizeof(entry->bitmaps[0]));
572 entry->gis = HeapReAlloc(GetProcessHeap(),
575 entry->nrealized * sizeof(entry->gis[0]));
577 entry->gis = HeapAlloc(GetProcessHeap(),
579 entry->nrealized * sizeof(entry->gis[0]));
585 ggo_format |= WINE_GGO_GRAY16_BITMAP;
589 ERR("aa = %d - not implemented\n", entry->aa);
591 ggo_format |= GGO_BITMAP;
595 buflen = GetGlyphOutlineW(physDev->hdc, glyph, ggo_format, &gm, 0, NULL,
597 if(buflen == GDI_ERROR) {
598 LeaveCriticalSection(&xrender_cs);
602 entry->realized[glyph] = TRUE;
604 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buflen);
605 GetGlyphOutlineW(physDev->hdc, glyph, ggo_format, &gm, buflen, buf, NULL);
607 TRACE("buflen = %d. Got metrics: %dx%d adv=%d,%d origin=%ld,%ld\n",
609 gm.gmBlackBoxX, gm.gmBlackBoxY, gm.gmCellIncX, gm.gmCellIncY,
610 gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
612 gi.width = gm.gmBlackBoxX;
613 gi.height = gm.gmBlackBoxY;
614 gi.x = -gm.gmptGlyphOrigin.x;
615 gi.y = gm.gmptGlyphOrigin.y;
616 gi.xOff = gm.gmCellIncX;
617 gi.yOff = gm.gmCellIncY;
619 if(TRACE_ON(xrender)) {
624 if(entry->aa == AA_None) {
625 pitch = ((gi.width + 31) / 32) * 4;
626 for(i = 0; i < gi.height; i++) {
627 line = buf + i * pitch;
629 for(j = 0; j < pitch * 8; j++) {
630 strcat(output, (line[j / 8] & (1 << (7 - (j % 8)))) ? "#" : " ");
632 strcat(output, "\n");
636 char blks[] = " .:;!o*#";
640 pitch = ((gi.width + 3) / 4) * 4;
641 for(i = 0; i < gi.height; i++) {
642 line = buf + i * pitch;
644 for(j = 0; j < pitch; j++) {
645 str[0] = blks[line[j] >> 5];
648 strcat(output, "\n");
654 if(entry->glyphset) {
655 if(entry->aa == AA_None && BitmapBitOrder(gdi_display) != MSBFirst) {
656 unsigned char *byte = buf, c;
662 /* magic to flip bit order */
663 c = ((c << 1) & 0xaa) | ((c >> 1) & 0x55);
664 c = ((c << 2) & 0xcc) | ((c >> 2) & 0x33);
665 c = ((c << 4) & 0xf0) | ((c >> 4) & 0x0f);
672 pXRenderAddGlyphs(gdi_display, entry->glyphset, &gid, &gi, 1,
675 HeapFree(GetProcessHeap(), 0, buf);
677 entry->bitmaps[glyph] = buf;
678 memcpy(&entry->gis[glyph], &gi, sizeof(gi));
683 static void SharpGlyphMono(X11DRV_PDEVICE *physDev, INT x, INT y,
684 void *bitmap, XGlyphInfo *gi)
686 unsigned char *srcLine = bitmap, *src;
687 unsigned char bits, bitsMask;
688 int width = gi->width;
689 int stride = ((width + 31) & ~31) >> 3;
690 int height = gi->height;
694 TRACE("%d, %d\n", x, y);
703 bitsMask = 0x80; /* FreeType is always MSB first */
717 bitsMask = bitsMask >> 1;
723 } while (bits & bitsMask);
724 XFillRectangle (gdi_display, physDev->drawable,
725 physDev->gc, xspan, y, lenspan, 1);
737 bitsMask = bitsMask >> 1;
743 } while (!(bits & bitsMask));
750 static void SharpGlyphGray(X11DRV_PDEVICE *physDev, INT x, INT y,
751 void *bitmap, XGlyphInfo *gi)
753 unsigned char *srcLine = bitmap, *src, bits;
754 int width = gi->width;
755 int stride = ((width + 3) & ~3);
756 int height = gi->height;
781 } while (bits >= 0x80);
782 XFillRectangle (gdi_display, physDev->drawable,
783 physDev->gc, xspan, y, lenspan, 1);
796 } while (bits < 0x80);
804 static void ExamineBitfield (DWORD mask, int *shift, int *len)
809 while ((mask & 1) == 0)
815 while ((mask & 1) == 1)
824 static DWORD GetField (DWORD pixel, int shift, int len)
826 pixel = pixel & (((1 << (len)) - 1) << shift);
827 pixel = pixel << (32 - (shift + len)) >> 24;
830 pixel |= (pixel >> len);
837 static DWORD PutField (DWORD pixel, int shift, int len)
839 shift = shift - (8 - len);
841 pixel &= (((1 << len) - 1) << (8 - len));
849 static void SmoothGlyphGray(XImage *image, int x, int y, void *bitmap, XGlyphInfo *gi,
855 BYTE *maskLine, *mask, m;
860 BYTE src_r, src_g, src_b;
862 src_r = GetRValue(color);
863 src_g = GetGValue(color);
864 src_b = GetBValue(color);
871 maskLine = (unsigned char *) bitmap;
872 maskStride = (width + 3) & ~3;
874 ExamineBitfield (image->red_mask, &r_shift, &r_len);
875 ExamineBitfield (image->green_mask, &g_shift, &g_len);
876 ExamineBitfield (image->blue_mask, &b_shift, &b_len);
880 maskLine += maskStride;
885 if(y >= image->height) break;
889 if(tx >= image->width) break;
896 pixel = (PutField ((src_r), r_shift, r_len) |
897 PutField ((src_g), g_shift, g_len) |
898 PutField ((src_b), b_shift, b_len));
899 XPutPixel (image, tx, y, pixel);
905 pixel = XGetPixel (image, tx, y);
907 r = GetField(pixel, r_shift, r_len);
908 r = ((BYTE)~m * (WORD)r + (BYTE)m * (WORD)src_r) >> 8;
909 g = GetField(pixel, g_shift, g_len);
910 g = ((BYTE)~m * (WORD)g + (BYTE)m * (WORD)src_g) >> 8;
911 b = GetField(pixel, b_shift, b_len);
912 b = ((BYTE)~m * (WORD)b + (BYTE)m * (WORD)src_b) >> 8;
914 pixel = (PutField (r, r_shift, r_len) |
915 PutField (g, g_shift, g_len) |
916 PutField (b, b_shift, b_len));
917 XPutPixel (image, tx, y, pixel);
923 static int XRenderErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
928 /***********************************************************************
929 * X11DRV_XRender_ExtTextOut
931 BOOL X11DRV_XRender_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
932 const RECT *lprect, LPCWSTR wstr, UINT count,
941 BOOL done_extents = FALSE;
942 INT width, xwidth, ywidth;
943 double cosEsc, sinEsc;
946 int render_op = PictOpOver;
951 HDC hdc = physDev->hdc;
952 DC *dc = physDev->dc;
953 int textPixel, backgroundPixel;
956 UINT align = GetTextAlign( hdc );
958 TRACE("%p, %d, %d, %08x, %p, %s, %d, %p)\n", hdc, x, y, flags,
959 lprect, debugstr_wn(wstr, count), count, lpDx);
961 if(flags & ETO_GLYPH_INDEX)
962 glyphs = (LPWORD)wstr;
964 glyphs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
965 GetGlyphIndicesW(hdc, wstr, count, glyphs, 0);
969 TRACE("rect: %ld,%ld - %ld,%ld\n", lprect->left, lprect->top, lprect->right,
971 TRACE("align = %x bkmode = %x mapmode = %x\n", align, GetBkMode(hdc), dc->MapMode);
973 if(align & TA_UPDATECP)
975 GetCurrentPositionEx( hdc, &pt );
980 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
981 if(lf.lfEscapement != 0) {
982 cosEsc = cos(lf.lfEscapement * M_PI / 1800);
983 sinEsc = sin(lf.lfEscapement * M_PI / 1800);
989 if(flags & (ETO_CLIPPED | ETO_OPAQUE)) {
991 if(flags & ETO_CLIPPED) return FALSE;
992 GetTextExtentPointI(hdc, glyphs, count, &sz);
996 rc.right = x + sz.cx;
997 rc.bottom = y + sz.cy;
1002 LPtoDP(hdc, (POINT*)&rc, 2);
1004 if(rc.left > rc.right) {INT tmp = rc.left; rc.left = rc.right; rc.right = tmp;}
1005 if(rc.top > rc.bottom) {INT tmp = rc.top; rc.top = rc.bottom; rc.bottom = tmp;}
1008 xgcval.function = GXcopy;
1009 xgcval.background = physDev->backgroundPixel;
1010 xgcval.fill_style = FillSolid;
1012 XChangeGC( gdi_display, physDev->gc, GCFunction | GCBackground | GCFillStyle, &xgcval );
1013 wine_tsx11_unlock();
1015 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
1017 if(physDev->depth == 1) {
1018 if((dc->textColor & 0xffffff) == 0) {
1020 backgroundPixel = 1;
1023 backgroundPixel = 0;
1026 textPixel = physDev->textPixel;
1027 backgroundPixel = physDev->backgroundPixel;
1030 if(flags & ETO_OPAQUE) {
1032 XSetForeground( gdi_display, physDev->gc, backgroundPixel );
1033 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
1034 physDev->org.x + rc.left, physDev->org.y + rc.top,
1035 rc.right - rc.left, rc.bottom - rc.top );
1036 wine_tsx11_unlock();
1046 LPtoDP(hdc, &pt, 1);
1050 TRACE("real x,y %d,%d\n", x, y);
1052 if((char_extra = GetTextCharacterExtra(hdc)) != 0) {
1055 deltas = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
1056 for(i = 0; i < count; i++) {
1058 deltas[i] = lpDx[i] + char_extra;
1060 GetTextExtentPointI(hdc, glyphs + i, 1, &tmpsz);
1061 deltas[i] = tmpsz.cx;
1065 deltas = (INT*)lpDx;
1069 for(idx = 0; idx < count; idx++)
1070 width += deltas[idx];
1073 GetTextExtentPointI(hdc, glyphs, count, &sz);
1074 done_extents = TRUE;
1078 width = X11DRV_XWStoDS(physDev, width);
1079 xwidth = width * cosEsc;
1080 ywidth = width * sinEsc;
1082 GetTextMetricsW(hdc, &tm);
1084 tm.tmAscent = X11DRV_YWStoDS(physDev, tm.tmAscent);
1085 tm.tmDescent = X11DRV_YWStoDS(physDev, tm.tmDescent);
1086 switch( dc->textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) ) {
1088 if (align & TA_UPDATECP) {
1091 DPtoLP(hdc, &pt, 1);
1092 MoveToEx(hdc, pt.x, pt.y, NULL);
1104 if (align & TA_UPDATECP) {
1107 DPtoLP(hdc, &pt, 1);
1108 MoveToEx(hdc, pt.x, pt.y, NULL);
1113 switch( align & (TA_TOP | TA_BOTTOM | TA_BASELINE) ) {
1115 y += tm.tmAscent * cosEsc;
1116 x += tm.tmAscent * sinEsc;
1120 y -= tm.tmDescent * cosEsc;
1121 x -= tm.tmDescent * sinEsc;
1128 if (flags & ETO_CLIPPED)
1130 SaveVisRgn16( HDC_16(hdc) );
1131 IntersectVisRect16( HDC_16(hdc), lprect->left, lprect->top, lprect->right, lprect->bottom );
1134 if(X11DRV_XRender_Installed) {
1135 if(!physDev->xrender->pict) {
1136 XRenderPictureAttributes pa;
1137 pa.subwindow_mode = IncludeInferiors;
1140 physDev->xrender->pict = pXRenderCreatePicture(gdi_display,
1142 (physDev->depth == 1) ?
1143 mono_format : screen_format,
1144 CPSubwindowMode, &pa);
1145 wine_tsx11_unlock();
1147 TRACE("allocing pict = %lx dc = %p drawable = %08lx\n", physDev->xrender->pict, dc, physDev->drawable);
1149 TRACE("using existing pict = %lx dc = %p drawable = %08lx\n", physDev->xrender->pict, dc, physDev->drawable);
1152 if ((data = X11DRV_GetRegionData( dc->hGCClipRgn, 0 )))
1155 pXRenderSetPictureClipRectangles( gdi_display, physDev->xrender->pict,
1156 physDev->org.x, physDev->org.y,
1157 (XRectangle *)data->Buffer, data->rdh.nCount );
1158 wine_tsx11_unlock();
1159 HeapFree( GetProcessHeap(), 0, data );
1163 if(GetBkMode(hdc) != TRANSPARENT) {
1164 if(!((flags & ETO_CLIPPED) && (flags & ETO_OPAQUE))) {
1165 if(!(flags & ETO_OPAQUE) || x < rc.left || x + width >= rc.right ||
1166 y - tm.tmAscent < rc.top || y + tm.tmDescent >= rc.bottom) {
1168 XSetForeground( gdi_display, physDev->gc, backgroundPixel );
1169 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
1170 physDev->org.x + x, physDev->org.y + y - tm.tmAscent,
1171 width, tm.tmAscent + tm.tmDescent );
1172 wine_tsx11_unlock();
1177 if(X11DRV_XRender_Installed) {
1178 /* Create a 1x1 pixmap to tile over the font mask */
1179 if(!physDev->xrender->tile_xpm) {
1180 XRenderPictureAttributes pa;
1182 XRenderPictFormat *format = (physDev->depth == 1) ? mono_format : screen_format;
1184 physDev->xrender->tile_xpm = XCreatePixmap(gdi_display,
1189 physDev->xrender->tile_pict = pXRenderCreatePicture(gdi_display,
1190 physDev->xrender->tile_xpm,
1193 wine_tsx11_unlock();
1194 TRACE("Created pixmap of depth %d\n", format->depth);
1195 /* init lastTextColor to something different from dc->textColor */
1196 physDev->xrender->lastTextColor = ~dc->textColor;
1200 if(dc->textColor != physDev->xrender->lastTextColor) {
1201 if(physDev->depth != 1) {
1202 /* Map 0 -- 0xff onto 0 -- 0xffff */
1203 col.red = GetRValue(dc->textColor);
1204 col.red |= col.red << 8;
1205 col.green = GetGValue(dc->textColor);
1206 col.green |= col.green << 8;
1207 col.blue = GetBValue(dc->textColor);
1208 col.blue |= col.blue << 8;
1210 } else { /* for a 1bpp bitmap we always need a 1 in the tile */
1211 col.red = col.green = col.blue = 0;
1215 pXRenderFillRectangle(gdi_display, PictOpSrc,
1216 physDev->xrender->tile_pict,
1218 wine_tsx11_unlock();
1219 physDev->xrender->lastTextColor = dc->textColor;
1222 /* FIXME the mapping of Text/BkColor onto 1 or 0 needs investigation.
1224 if((physDev->depth == 1) && (textPixel == 0))
1225 render_op = PictOpOutReverse; /* This gives us 'black' text */
1228 EnterCriticalSection(&xrender_cs);
1229 entry = glyphsetCache + physDev->xrender->cache_index;
1231 for(idx = 0; idx < count; idx++) {
1232 if(glyphs[idx] >= entry->nrealized || entry->realized[glyphs[idx]] == FALSE) {
1233 UploadGlyph(physDev, glyphs[idx]);
1238 TRACE("Writing %s at %ld,%ld\n", debugstr_wn(wstr,count),
1239 physDev->org.x + x, physDev->org.y + y);
1241 if(X11DRV_XRender_Installed) {
1244 pXRenderCompositeString16(gdi_display, render_op,
1245 physDev->xrender->tile_pict,
1246 physDev->xrender->pict,
1247 entry->font_format, entry->glyphset,
1248 0, 0, physDev->org.x + x, physDev->org.y + y,
1252 INT offset = 0, xoff = 0, yoff = 0;
1253 for(idx = 0; idx < count; idx++) {
1254 pXRenderCompositeString16(gdi_display, render_op,
1255 physDev->xrender->tile_pict,
1256 physDev->xrender->pict,
1257 entry->font_format, entry->glyphset,
1258 0, 0, physDev->org.x + x + xoff,
1259 physDev->org.y + y + yoff,
1261 offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1262 xoff = offset * cosEsc;
1263 yoff = offset * -sinEsc;
1266 wine_tsx11_unlock();
1268 if (lf.lfUnderline || lf.lfStrikeOut) {
1270 unsigned int lineWidth;
1271 UINT nMetricsSize = GetOutlineTextMetricsW(hdc, 0, NULL);
1272 OUTLINETEXTMETRICW* otm = HeapAlloc(GetProcessHeap(), 0, nMetricsSize);
1273 if (!otm) goto done;
1275 GetOutlineTextMetricsW(hdc, nMetricsSize, otm);
1278 XSetForeground( gdi_display, physDev->gc, physDev->textPixel );
1280 if (lf.lfUnderline) {
1281 linePos = X11DRV_YWStoDS(physDev, otm->otmsUnderscorePosition);
1282 lineWidth = X11DRV_YWStoDS(physDev, otm->otmsUnderscoreSize);
1284 XSetLineAttributes( gdi_display, physDev->gc, lineWidth,
1285 LineSolid, CapProjecting, JoinBevel );
1286 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1287 physDev->org.x + x, physDev->org.y + y - linePos,
1288 physDev->org.x + x + width, physDev->org.y + y - linePos );
1291 if (lf.lfStrikeOut) {
1292 linePos = X11DRV_YWStoDS(physDev, otm->otmsStrikeoutPosition);
1293 lineWidth = X11DRV_YWStoDS(physDev, otm->otmsStrikeoutSize);
1295 XSetLineAttributes( gdi_display, physDev->gc, lineWidth,
1296 LineSolid, CapProjecting, JoinBevel );
1297 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1298 physDev->org.x + x, physDev->org.y + y - linePos,
1299 physDev->org.x + x + width, physDev->org.y + y - linePos );
1301 wine_tsx11_unlock();
1302 HeapFree(GetProcessHeap(), 0, otm);
1306 INT offset = 0, xoff = 0, yoff = 0;
1308 XSetForeground( gdi_display, physDev->gc, textPixel );
1310 if(entry->aa == AA_None) {
1311 for(idx = 0; idx < count; idx++) {
1312 SharpGlyphMono(physDev, physDev->org.x + x + xoff,
1313 physDev->org.y + y + yoff,
1314 entry->bitmaps[glyphs[idx]],
1315 &entry->gis[glyphs[idx]]);
1317 offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1318 xoff = offset * cosEsc;
1319 yoff = offset * -sinEsc;
1322 xoff += entry->gis[glyphs[idx]].xOff;
1323 yoff += entry->gis[glyphs[idx]].yOff;
1326 } else if(physDev->depth == 1) {
1327 for(idx = 0; idx < count; idx++) {
1328 SharpGlyphGray(physDev, physDev->org.x + x + xoff,
1329 physDev->org.y + y + yoff,
1330 entry->bitmaps[glyphs[idx]],
1331 &entry->gis[glyphs[idx]]);
1333 offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1334 xoff = offset * cosEsc;
1335 yoff = offset * -sinEsc;
1338 xoff += entry->gis[glyphs[idx]].xOff;
1339 yoff += entry->gis[glyphs[idx]].yOff;
1345 unsigned int w, h, dummy_uint;
1346 Window dummy_window;
1348 int image_x, image_y, image_off_x, image_off_y, image_w, image_h;
1349 RECT extents = {0, 0, 0, 0};
1353 XGetGeometry(gdi_display, physDev->drawable, &dummy_window, &dummy_int, &dummy_int,
1354 &w, &h, &dummy_uint, &dummy_uint);
1355 TRACE("drawable %dx%d\n", w, h);
1357 for(idx = 0; idx < count; idx++) {
1358 if(extents.left > cur.x - entry->gis[glyphs[idx]].x)
1359 extents.left = cur.x - entry->gis[glyphs[idx]].x;
1360 if(extents.top > cur.y - entry->gis[glyphs[idx]].y)
1361 extents.top = cur.y - entry->gis[glyphs[idx]].y;
1362 if(extents.right < cur.x - entry->gis[glyphs[idx]].x + entry->gis[glyphs[idx]].width)
1363 extents.right = cur.x - entry->gis[glyphs[idx]].x + entry->gis[glyphs[idx]].width;
1364 if(extents.bottom < cur.y - entry->gis[glyphs[idx]].y + entry->gis[glyphs[idx]].height)
1365 extents.bottom = cur.y - entry->gis[glyphs[idx]].y + entry->gis[glyphs[idx]].height;
1367 offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1368 cur.x = offset * cosEsc;
1369 cur.y = offset * -sinEsc;
1371 cur.x += entry->gis[glyphs[idx]].xOff;
1372 cur.y += entry->gis[glyphs[idx]].yOff;
1375 TRACE("glyph extents %ld,%ld - %ld,%ld drawable x,y %ld,%ld\n", extents.left, extents.top,
1376 extents.right, extents.bottom, physDev->org.x + x, physDev->org.y + y);
1378 if(physDev->org.x + x + extents.left >= 0) {
1379 image_x = physDev->org.x + x + extents.left;
1383 image_off_x = physDev->org.x + x + extents.left;
1385 if(physDev->org.y + y + extents.top >= 0) {
1386 image_y = physDev->org.y + y + extents.top;
1390 image_off_y = physDev->org.y + y + extents.top;
1392 if(physDev->org.x + x + extents.right < w)
1393 image_w = physDev->org.x + x + extents.right - image_x;
1395 image_w = w - image_x;
1396 if(physDev->org.y + y + extents.bottom < h)
1397 image_h = physDev->org.y + y + extents.bottom - image_y;
1399 image_h = h - image_y;
1401 if(image_w <= 0 || image_h <= 0) goto no_image;
1403 X11DRV_expect_error(gdi_display, XRenderErrorHandler, NULL);
1404 image = XGetImage(gdi_display, physDev->drawable,
1405 image_x, image_y, image_w, image_h,
1406 AllPlanes, ZPixmap);
1407 X11DRV_check_error();
1409 TRACE("XGetImage(%p, %x, %d, %d, %d, %d, %lx, %x) depth = %d rets %p\n",
1410 gdi_display, (int)physDev->drawable, image_x, image_y,
1411 image_w, image_h, AllPlanes, ZPixmap,
1412 physDev->depth, image);
1414 Pixmap xpm = XCreatePixmap(gdi_display, physDev->drawable, image_w, image_h,
1419 gcv.graphics_exposures = False;
1420 gc = XCreateGC(gdi_display, xpm, GCGraphicsExposures, &gcv);
1421 XCopyArea(gdi_display, physDev->drawable, xpm, gc, image_x, image_y,
1422 image_w, image_h, 0, 0);
1423 XFreeGC(gdi_display, gc);
1424 X11DRV_expect_error(gdi_display, XRenderErrorHandler, NULL);
1425 image = XGetImage(gdi_display, xpm, 0, 0, image_w, image_h, AllPlanes,
1427 X11DRV_check_error();
1428 XFreePixmap(gdi_display, xpm);
1430 if(!image) goto no_image;
1432 image->red_mask = visual->red_mask;
1433 image->green_mask = visual->green_mask;
1434 image->blue_mask = visual->blue_mask;
1436 offset = xoff = yoff = 0;
1437 for(idx = 0; idx < count; idx++) {
1438 SmoothGlyphGray(image, xoff + image_off_x - extents.left,
1439 yoff + image_off_y - extents.top,
1440 entry->bitmaps[glyphs[idx]],
1441 &entry->gis[glyphs[idx]],
1444 offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1445 xoff = offset * cosEsc;
1446 yoff = offset * -sinEsc;
1448 xoff += entry->gis[glyphs[idx]].xOff;
1449 yoff += entry->gis[glyphs[idx]].yOff;
1453 XPutImage(gdi_display, physDev->drawable, physDev->gc, image, 0, 0,
1454 image_x, image_y, image_w, image_h);
1455 XDestroyImage(image);
1458 wine_tsx11_unlock();
1460 LeaveCriticalSection(&xrender_cs);
1462 if(deltas && deltas != lpDx)
1463 HeapFree(GetProcessHeap(), 0, deltas);
1465 if (flags & ETO_CLIPPED)
1466 RestoreVisRgn16( HDC_16(hdc) );
1471 X11DRV_UnlockDIBSection( physDev, TRUE );
1472 if(glyphs != wstr) HeapFree(GetProcessHeap(), 0, glyphs);
1476 #else /* HAVE_X11_EXTENSIONS_XRENDER_H */
1478 void X11DRV_XRender_Init(void)
1480 TRACE("XRender support not compiled in.\n");
1484 void X11DRV_XRender_Finalize(void)
1488 BOOL X11DRV_XRender_SelectFont(X11DRV_PDEVICE *physDev, HFONT hfont)
1494 void X11DRV_XRender_DeleteDC(X11DRV_PDEVICE *physDev)
1500 BOOL X11DRV_XRender_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
1501 const RECT *lprect, LPCWSTR wstr, UINT count,
1508 void X11DRV_XRender_UpdateDrawable(X11DRV_PDEVICE *physDev)
1514 #endif /* HAVE_X11_EXTENSIONS_XRENDER_H */