kernel: Remove no longer needed includes.
[wine] / dlls / x11drv / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 "winnls.h"
30 #include "x11font.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(text);
34
35 #define SWAP_INT(a,b)  { int t = a; a = b; b = t; }
36 #define IROUND(x) (int)((x)>0? (x)+0.5 : (x) - 0.5)
37
38
39 /***********************************************************************
40  *           X11DRV_ExtTextOut
41  */
42 BOOL
43 X11DRV_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
44                    const RECT *lprect, LPCWSTR wstr, UINT count,
45                    const INT *lpDx )
46 {
47     unsigned int i;
48     fontObject*         pfo;
49     XFontStruct*        font;
50     BOOL                rotated = FALSE;
51     XChar2b             *str2b = NULL;
52     BOOL                dibUpdateFlag = FALSE;
53     BOOL                result = TRUE;
54     HRGN                saved_region = 0;
55
56     if(physDev->has_gdi_font)
57         return X11DRV_XRender_ExtTextOut(physDev, x, y, flags, lprect, wstr, count, lpDx);
58
59     if (!X11DRV_SetupGCForText( physDev )) return TRUE;
60
61     pfo = XFONT_GetFontObject( physDev->font );
62     font = pfo->fs;
63
64     if (pfo->lf.lfEscapement && pfo->lpX11Trans)
65         rotated = TRUE;
66
67     TRACE("hdc=%p df=%04x %d,%d %s, %d  flags=%d lpDx=%p\n",
68           physDev->hdc, (UINT16)(physDev->font), x, y,
69           debugstr_wn (wstr, count), count, flags, lpDx);
70
71     if (lprect != NULL) TRACE("\trect=(%ld,%ld - %ld,%ld)\n",
72                                      lprect->left, lprect->top,
73                                      lprect->right, lprect->bottom );
74
75       /* Draw the rectangle */
76
77     if (flags & ETO_OPAQUE)
78     {
79         X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
80         dibUpdateFlag = TRUE;
81         wine_tsx11_lock();
82         XSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
83         XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
84                         physDev->org.x + lprect->left, physDev->org.y + lprect->top,
85                         lprect->right - lprect->left, lprect->bottom - lprect->top );
86         wine_tsx11_unlock();
87     }
88     if (!count) goto END;  /* Nothing more to do */
89
90
91       /* Set the clip region */
92
93     if (flags & ETO_CLIPPED)
94     {
95         HRGN clip_region;
96
97         clip_region = CreateRectRgnIndirect( lprect );
98         /* make a copy of the current device region */
99         saved_region = CreateRectRgn( 0, 0, 0, 0 );
100         CombineRgn( saved_region, physDev->region, 0, RGN_COPY );
101         X11DRV_SetDeviceClipping( physDev, saved_region, clip_region );
102         DeleteObject( clip_region );
103     }
104
105       /* Draw the text background if necessary */
106
107     if (!dibUpdateFlag)
108     {
109         X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
110         dibUpdateFlag = TRUE;
111     }
112
113
114     /* Draw the text (count > 0 verified) */
115     if (!(str2b = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, wstr, count )))
116         goto FAIL;
117
118     wine_tsx11_lock();
119     XSetForeground( gdi_display, physDev->gc, physDev->textPixel );
120     wine_tsx11_unlock();
121     if(!rotated)
122     {
123         if (!lpDx)
124         {
125             X11DRV_cptable[pfo->fi->cptable].pDrawString(
126                            pfo, gdi_display, physDev->drawable, physDev->gc,
127                            physDev->org.x + x, physDev->org.y + y, str2b, count );
128         }
129         else
130         {
131             XTextItem16 *items, *pitem;
132
133             pitem = items = HeapAlloc( GetProcessHeap(), 0,
134                                        count * sizeof(XTextItem16) );
135             if(items == NULL) goto FAIL;
136
137             for(i = 0; i < count; i++)
138             {
139                 pitem->chars  = str2b + i;
140                 pitem->delta  = lpDx[i];
141                 pitem->nchars = 1;
142                 pitem->font   = None;
143                 pitem++;
144             }
145
146             X11DRV_cptable[pfo->fi->cptable].pDrawText( pfo, gdi_display,
147                                   physDev->drawable, physDev->gc,
148                                   physDev->org.x + x, physDev->org.y + y, items, pitem - items );
149             HeapFree( GetProcessHeap(), 0, items );
150         }
151     }
152     else /* rotated */
153     {
154         /* have to render character by character. */
155         double offset = 0.0;
156         int i;
157
158         for (i=0; i<count; i++)
159         {
160             int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8)
161                 - font->min_char_or_byte2;
162             int x_i = IROUND((double) (physDev->org.x + x) + offset *
163                              pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
164             int y_i = IROUND((double) (physDev->org.y + y) - offset *
165                              pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
166
167             X11DRV_cptable[pfo->fi->cptable].pDrawString(
168                                     pfo, gdi_display, physDev->drawable, physDev->gc,
169                                     x_i, y_i, &str2b[i], 1);
170             if (lpDx)
171             {
172                 offset += lpDx[i];
173             }
174             else
175             {
176                 offset += (double) (font->per_char ?
177                                     font->per_char[char_metric_offset].attributes:
178                                     font->min_bounds.attributes)
179                     * pfo->lpX11Trans->pixelsize / 1000.0;
180             }
181         }
182     }
183     HeapFree( GetProcessHeap(), 0, str2b );
184
185     if (flags & ETO_CLIPPED)
186     {
187         /* restore the device region */
188         X11DRV_SetDeviceClipping( physDev, saved_region, 0 );
189         DeleteObject( saved_region );
190     }
191     goto END;
192
193 FAIL:
194     HeapFree( GetProcessHeap(), 0, str2b );
195     result = FALSE;
196
197 END:
198     if (dibUpdateFlag) X11DRV_UnlockDIBSection( physDev, TRUE );
199     return result;
200 }
201
202
203 /***********************************************************************
204  *           X11DRV_GetTextExtentPoint
205  */
206 BOOL X11DRV_GetTextExtentPoint( X11DRV_PDEVICE *physDev, LPCWSTR str, INT count,
207                                   LPSIZE size )
208 {
209     fontObject* pfo = XFONT_GetFontObject( physDev->font );
210
211     TRACE("%s %d\n", debugstr_wn(str,count), count);
212     if( pfo ) {
213         XChar2b *p = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, str, count );
214         if (!p) return FALSE;
215         if( !pfo->lpX11Trans ) {
216             int dir, ascent, descent;
217             int info_width;
218             X11DRV_cptable[pfo->fi->cptable].pTextExtents( pfo, p,
219                                 count, &dir, &ascent, &descent, &info_width );
220
221           size->cx = info_width;
222           size->cy = pfo->fs->ascent + pfo->fs->descent;
223         } else {
224             INT i;
225             float x = 0.0, y = 0.0;
226             /* FIXME: Deal with *_char_or_byte2 != 0 situations */
227             for(i = 0; i < count; i++) {
228                 x += pfo->fs->per_char ?
229            pfo->fs->per_char[p[i].byte2 - pfo->fs->min_char_or_byte2].attributes :
230            pfo->fs->min_bounds.attributes;
231             }
232             y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
233             TRACE("x = %f y = %f\n", x, y);
234             size->cx = x * pfo->lpX11Trans->pixelsize / 1000.0;
235             size->cy = y * pfo->lpX11Trans->pixelsize / 1000.0;
236         }
237         size->cx *= pfo->rescale;
238         size->cy *= pfo->rescale;
239         HeapFree( GetProcessHeap(), 0, p );
240         return TRUE;
241     }
242     return FALSE;
243 }