iphlpapi: Add support for TCP_TABLE_OWNER_PID_ALL in GetExtendedTcpTable.
[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 X11DRV_ExtTextOut( PHYSDEV dev, INT x, INT y, UINT flags,
41                         const RECT *lprect, LPCWSTR wstr, UINT count, const INT *lpDx )
42 {
43     X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
44     BOOL restore_region = FALSE;
45     unsigned int i;
46     int pixel;
47     fontObject*         pfo = XFONT_GetFontObject( physDev->font );
48     XFontStruct*        font;
49     BOOL                rotated = FALSE;
50     XChar2b             *str2b = NULL;
51     BOOL                result = TRUE;
52
53     if (!pfo)
54     {
55         dev = GET_NEXT_PHYSDEV( dev, pExtTextOut );
56         return dev->funcs->pExtTextOut( dev, x, y, flags, lprect, wstr, count, lpDx );
57     }
58
59     if (!X11DRV_SetupGCForText( physDev )) return TRUE;
60
61     font = pfo->fs;
62
63     if (pfo->lf.lfEscapement && pfo->lpX11Trans)
64         rotated = TRUE;
65
66     TRACE("hdc=%p df=%04x %d,%d rc %s %s, %d  flags=%d lpDx=%p\n",
67           dev->hdc, (UINT16)physDev->font, x, y, wine_dbgstr_rect(lprect),
68           debugstr_wn (wstr, count), count, flags, lpDx);
69
70       /* Draw the rectangle */
71
72     if (flags & ETO_OPAQUE)
73     {
74         pixel = X11DRV_PALETTE_ToPhysical( physDev, GetBkColor(physDev->dev.hdc) );
75         wine_tsx11_lock();
76         XSetForeground( gdi_display, physDev->gc, pixel );
77         XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
78                         physDev->dc_rect.left + lprect->left, physDev->dc_rect.top + lprect->top,
79                         lprect->right - lprect->left, lprect->bottom - lprect->top );
80         wine_tsx11_unlock();
81     }
82     if (!count) goto END;  /* Nothing more to do */
83
84
85       /* Set the clip region */
86
87     if (flags & ETO_CLIPPED)
88     {
89         HRGN clip_region = CreateRectRgnIndirect( lprect );
90         restore_region = add_extra_clipping_region( physDev, clip_region );
91         DeleteObject( clip_region );
92     }
93
94
95     /* Draw the text (count > 0 verified) */
96     if (!(str2b = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, wstr, count )))
97     {
98         result = FALSE;
99         goto END;
100     }
101
102     pixel = X11DRV_PALETTE_ToPhysical( physDev, GetTextColor(physDev->dev.hdc) );
103     wine_tsx11_lock();
104     XSetForeground( gdi_display, physDev->gc, pixel );
105     wine_tsx11_unlock();
106     if(!rotated)
107     {
108         if (!lpDx)
109         {
110             X11DRV_cptable[pfo->fi->cptable].pDrawString(
111                            pfo, gdi_display, physDev->drawable, physDev->gc,
112                            physDev->dc_rect.left + x, physDev->dc_rect.top + y, str2b, count );
113         }
114         else
115         {
116             XTextItem16 *items;
117
118             items = HeapAlloc( GetProcessHeap(), 0, count * sizeof(XTextItem16) );
119             if(items == NULL)
120             {
121                 result = FALSE;
122                 goto END;
123             }
124             items[0].chars = str2b;
125             items[0].delta = 0;
126             items[0].nchars = 1;
127             items[0].font = None;
128             for(i = 1; i < count; i++)
129             {
130                 items[i].chars  = str2b + i;
131                 items[i].delta  = (flags & ETO_PDY) ? lpDx[(i - 1) * 2] : lpDx[i - 1];
132                 items[i].delta -= X11DRV_cptable[pfo->fi->cptable].pTextWidth( pfo, str2b + i - 1, 1 );
133                 items[i].nchars = 1;
134                 items[i].font   = None;
135             }
136             X11DRV_cptable[pfo->fi->cptable].pDrawText( pfo, gdi_display,
137                                   physDev->drawable, physDev->gc,
138                                   physDev->dc_rect.left + x, physDev->dc_rect.top + y, items, count );
139             HeapFree( GetProcessHeap(), 0, items );
140         }
141     }
142     else /* rotated */
143     {
144         /* have to render character by character. */
145         double offset = 0.0;
146         UINT i;
147
148         for (i=0; i<count; i++)
149         {
150             int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8)
151                 - font->min_char_or_byte2;
152             int x_i = IROUND((double) (physDev->dc_rect.left + x) + offset *
153                              pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
154             int y_i = IROUND((double) (physDev->dc_rect.top + y) - offset *
155                              pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
156
157             X11DRV_cptable[pfo->fi->cptable].pDrawString(
158                                     pfo, gdi_display, physDev->drawable, physDev->gc,
159                                     x_i, y_i, &str2b[i], 1);
160             if (lpDx)
161             {
162                 offset += (flags & ETO_PDY) ? lpDx[i * 2] : lpDx[i];
163             }
164             else
165             {
166                 offset += (double) (font->per_char ?
167                                     font->per_char[char_metric_offset].attributes:
168                                     font->min_bounds.attributes)
169                     * pfo->lpX11Trans->pixelsize / 1000.0;
170             }
171         }
172     }
173
174 END:
175     HeapFree( GetProcessHeap(), 0, str2b );
176     if (restore_region) restore_clipping_region( physDev );
177     return result;
178 }
179
180
181 /***********************************************************************
182  *           X11DRV_GetTextExtentExPoint
183  */
184 BOOL X11DRV_GetTextExtentExPoint( PHYSDEV dev, LPCWSTR str, INT count,
185                                   INT maxExt, LPINT lpnFit, LPINT alpDx, LPSIZE size )
186 {
187     X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
188     fontObject* pfo = XFONT_GetFontObject( physDev->font );
189
190     TRACE("%s %d\n", debugstr_wn(str,count), count);
191     if( pfo ) {
192         XChar2b *p = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, str, count );
193         if (!p) return FALSE;
194         if( !pfo->lpX11Trans ) {
195             int dir, ascent, descent;
196             int info_width;
197             X11DRV_cptable[pfo->fi->cptable].pTextExtents( pfo, p,
198                                 count, &dir, &ascent, &descent, &info_width,
199                                 maxExt, lpnFit, alpDx );
200
201           size->cx = info_width;
202           size->cy = pfo->fs->ascent + pfo->fs->descent;
203         } else {
204             INT i;
205             INT nfit = 0;
206             float x = 0.0, y = 0.0;
207             float scaled_x = 0.0, pixsize = pfo->lpX11Trans->pixelsize;
208             /* FIXME: Deal with *_char_or_byte2 != 0 situations */
209             for(i = 0; i < count; i++) {
210                 x += pfo->fs->per_char ?
211            pfo->fs->per_char[p[i].byte2 - pfo->fs->min_char_or_byte2].attributes :
212            pfo->fs->min_bounds.attributes;
213                 scaled_x = x * pixsize / 1000.0;
214                 if (alpDx)
215                     alpDx[i] = scaled_x;
216                 if (scaled_x <= maxExt)
217                     ++nfit;
218             }
219             y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
220             TRACE("x = %f y = %f\n", x, y);
221             size->cx = x * pfo->lpX11Trans->pixelsize / 1000.0;
222             size->cy = y * pfo->lpX11Trans->pixelsize / 1000.0;
223             if (lpnFit)
224                 *lpnFit = nfit;
225         }
226         size->cx *= pfo->rescale;
227         size->cy *= pfo->rescale;
228         HeapFree( GetProcessHeap(), 0, p );
229         return TRUE;
230     }
231
232     dev = GET_NEXT_PHYSDEV( dev, pGetTextExtentExPoint );
233     return dev->funcs->pGetTextExtentExPoint( dev, str, count, maxExt, lpnFit, alpDx, size );
234 }