winex11: Make sure that all glX functions are called under the X11 lock.
[wine] / dlls / winex11.drv / text.c
1 /*
2  * X11 graphics driver text functions
3  *
4  * Copyright 1993,1994 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <math.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "x11font.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(text);
33
34 #define IROUND(x) (int)((x)>0? (x)+0.5 : (x) - 0.5)
35
36
37 /***********************************************************************
38  *           X11DRV_ExtTextOut
39  */
40 BOOL
41 X11DRV_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
42                    const RECT *lprect, LPCWSTR wstr, UINT count,
43                    const INT *lpDx )
44 {
45     unsigned int i;
46     fontObject*         pfo;
47     XFontStruct*        font;
48     BOOL                rotated = FALSE;
49     XChar2b             *str2b = NULL;
50     BOOL                dibUpdateFlag = FALSE;
51     BOOL                result = TRUE;
52     HRGN                saved_region = 0;
53
54     if(physDev->has_gdi_font)
55         return X11DRV_XRender_ExtTextOut(physDev, x, y, flags, lprect, wstr, count, lpDx);
56
57     if (!X11DRV_SetupGCForText( physDev )) return TRUE;
58
59     pfo = XFONT_GetFontObject( physDev->font );
60     font = pfo->fs;
61
62     if (pfo->lf.lfEscapement && pfo->lpX11Trans)
63         rotated = TRUE;
64
65     TRACE("hdc=%p df=%04x %d,%d %s, %d  flags=%d lpDx=%p\n",
66           physDev->hdc, (UINT16)(physDev->font), x, y,
67           debugstr_wn (wstr, count), count, flags, lpDx);
68
69     if (lprect != NULL) TRACE("\trect=(%d,%d - %d,%d)\n",
70                                      lprect->left, lprect->top,
71                                      lprect->right, lprect->bottom );
72
73       /* Draw the rectangle */
74
75     if (flags & ETO_OPAQUE)
76     {
77         X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod );
78         dibUpdateFlag = TRUE;
79         wine_tsx11_lock();
80         XSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
81         XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
82                         physDev->dc_rect.left + lprect->left, physDev->dc_rect.top + lprect->top,
83                         lprect->right - lprect->left, lprect->bottom - lprect->top );
84         wine_tsx11_unlock();
85     }
86     if (!count) goto END;  /* Nothing more to do */
87
88
89       /* Set the clip region */
90
91     if (flags & ETO_CLIPPED)
92     {
93         HRGN clip_region;
94
95         clip_region = CreateRectRgnIndirect( lprect );
96         /* make a copy of the current device region */
97         saved_region = CreateRectRgn( 0, 0, 0, 0 );
98         CombineRgn( saved_region, physDev->region, 0, RGN_COPY );
99         X11DRV_SetDeviceClipping( physDev, saved_region, clip_region );
100         DeleteObject( clip_region );
101     }
102
103       /* Draw the text background if necessary */
104
105     if (!dibUpdateFlag)
106     {
107         X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod );
108         dibUpdateFlag = TRUE;
109     }
110
111
112     /* Draw the text (count > 0 verified) */
113     if (!(str2b = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, wstr, count )))
114         goto FAIL;
115
116     wine_tsx11_lock();
117     XSetForeground( gdi_display, physDev->gc, physDev->textPixel );
118     wine_tsx11_unlock();
119     if(!rotated)
120     {
121         if (!lpDx)
122         {
123             X11DRV_cptable[pfo->fi->cptable].pDrawString(
124                            pfo, gdi_display, physDev->drawable, physDev->gc,
125                            physDev->dc_rect.left + x, physDev->dc_rect.top + y, str2b, count );
126         }
127         else
128         {
129             XTextItem16 *items, *pitem;
130
131             pitem = items = HeapAlloc( GetProcessHeap(), 0,
132                                        count * sizeof(XTextItem16) );
133             if(items == NULL) goto FAIL;
134
135             for(i = 0; i < count; i++)
136             {
137                 pitem->chars  = str2b + i;
138                 pitem->delta  = lpDx[i];
139                 pitem->nchars = 1;
140                 pitem->font   = None;
141                 pitem++;
142             }
143
144             X11DRV_cptable[pfo->fi->cptable].pDrawText( pfo, gdi_display,
145                                   physDev->drawable, physDev->gc,
146                                   physDev->dc_rect.left + x, physDev->dc_rect.top + y, items, pitem - items );
147             HeapFree( GetProcessHeap(), 0, items );
148         }
149     }
150     else /* rotated */
151     {
152         /* have to render character by character. */
153         double offset = 0.0;
154         int i;
155
156         for (i=0; i<count; i++)
157         {
158             int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8)
159                 - font->min_char_or_byte2;
160             int x_i = IROUND((double) (physDev->dc_rect.left + x) + offset *
161                              pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
162             int y_i = IROUND((double) (physDev->dc_rect.top + y) - offset *
163                              pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
164
165             X11DRV_cptable[pfo->fi->cptable].pDrawString(
166                                     pfo, gdi_display, physDev->drawable, physDev->gc,
167                                     x_i, y_i, &str2b[i], 1);
168             if (lpDx)
169             {
170                 offset += lpDx[i];
171             }
172             else
173             {
174                 offset += (double) (font->per_char ?
175                                     font->per_char[char_metric_offset].attributes:
176                                     font->min_bounds.attributes)
177                     * pfo->lpX11Trans->pixelsize / 1000.0;
178             }
179         }
180     }
181     HeapFree( GetProcessHeap(), 0, str2b );
182
183     if (flags & ETO_CLIPPED)
184     {
185         /* restore the device region */
186         X11DRV_SetDeviceClipping( physDev, saved_region, 0 );
187         DeleteObject( saved_region );
188     }
189     goto END;
190
191 FAIL:
192     HeapFree( GetProcessHeap(), 0, str2b );
193     result = FALSE;
194
195 END:
196     if (dibUpdateFlag) X11DRV_UnlockDIBSection( physDev, TRUE );
197     return result;
198 }
199
200
201 /***********************************************************************
202  *           X11DRV_GetTextExtentExPoint
203  */
204 BOOL X11DRV_GetTextExtentExPoint( X11DRV_PDEVICE *physDev, LPCWSTR str, INT count,
205                                   INT maxExt, LPINT lpnFit, LPINT alpDx, LPSIZE size )
206 {
207     fontObject* pfo = XFONT_GetFontObject( physDev->font );
208
209     TRACE("%s %d\n", debugstr_wn(str,count), count);
210     if( pfo ) {
211         XChar2b *p = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, str, count );
212         if (!p) return FALSE;
213         if( !pfo->lpX11Trans ) {
214             int dir, ascent, descent;
215             int info_width;
216             X11DRV_cptable[pfo->fi->cptable].pTextExtents( pfo, p,
217                                 count, &dir, &ascent, &descent, &info_width,
218                                 maxExt, lpnFit, alpDx );
219
220           size->cx = info_width;
221           size->cy = pfo->fs->ascent + pfo->fs->descent;
222         } else {
223             INT i;
224             INT nfit = 0;
225             float x = 0.0, y = 0.0;
226             float scaled_x = 0.0, pixsize = pfo->lpX11Trans->pixelsize;
227             /* FIXME: Deal with *_char_or_byte2 != 0 situations */
228             for(i = 0; i < count; i++) {
229                 x += pfo->fs->per_char ?
230            pfo->fs->per_char[p[i].byte2 - pfo->fs->min_char_or_byte2].attributes :
231            pfo->fs->min_bounds.attributes;
232                 scaled_x = x * pixsize / 1000.0;
233                 if (alpDx)
234                     alpDx[i] = scaled_x;
235                 if (scaled_x <= maxExt)
236                     ++nfit;
237             }
238             y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
239             TRACE("x = %f y = %f\n", x, y);
240             size->cx = x * pfo->lpX11Trans->pixelsize / 1000.0;
241             size->cy = y * pfo->lpX11Trans->pixelsize / 1000.0;
242             if (lpnFit)
243                 *lpnFit = nfit;
244         }
245         size->cx *= pfo->rescale;
246         size->cy *= pfo->rescale;
247         HeapFree( GetProcessHeap(), 0, p );
248         return TRUE;
249     }
250     return FALSE;
251 }