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