Added regedit unit test, a couple minor changes to regedit.
[wine] / objects / text.c
1 /*
2  * 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 <string.h>
22
23 #include "windef.h"
24 #include "wingdi.h"
25 #include "wine/winuser16.h"
26 #include "winbase.h"
27 #include "winerror.h"
28 #include "gdi.h"
29 #include "wine/debug.h"
30 #include "winnls.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(text);
33
34 /***********************************************************************
35  *           FONT_mbtowc
36  *
37  * Returns a '\0' terminated Unicode translation of str using the
38  * charset of the currently selected font in hdc.  If count is -1 then
39  * str is assumed to be '\0' terminated, otherwise it contains the
40  * number of bytes to convert.  If plenW is non-NULL, on return it
41  * will point to the number of WCHARs (excluding the '\0') that have
42  * been written.  If pCP is non-NULL, on return it will point to the
43  * codepage used in the conversion.  The caller should free the
44  * returned LPWSTR from the process heap itself.
45  */
46 LPWSTR FONT_mbtowc(HDC hdc, LPCSTR str, INT count, INT *plenW, UINT *pCP)
47 {
48     UINT cp = CP_ACP;
49     INT lenW;
50     LPWSTR strW;
51     CHARSETINFO csi;
52     int charset = GetTextCharset(hdc);
53
54     /* Hmm, nicely designed api this one! */
55     if(TranslateCharsetInfo((DWORD*)charset, &csi, TCI_SRCCHARSET))
56         cp = csi.ciACP;
57     else {
58         switch(charset) {
59         case SYMBOL_CHARSET: /* We don't want any translation here */
60             cp = GetACP();
61             break;
62         case OEM_CHARSET:
63             cp = GetOEMCP();
64             break;
65         case DEFAULT_CHARSET:
66             cp = GetACP();
67             break;
68
69         case VISCII_CHARSET:
70         case TCVN_CHARSET:
71         case KOI8_CHARSET:
72         case ISO3_CHARSET:
73         case ISO4_CHARSET:
74         case ISO10_CHARSET:
75         case CELTIC_CHARSET:
76           /* FIXME: These have no place here, but because x11drv
77              enumerates fonts with these (made up) charsets some apps
78              might use them and then the FIXME below would become
79              annoying.  Now we could pick the intended codepage for
80              each of these, but since it's broken anyway we'll just
81              use CP_ACP and hope it'll go away...
82           */
83             cp = CP_ACP;
84             break;
85
86
87         default:
88             FIXME("Can't find codepage for charset %d\n", charset);
89             break;
90         }
91     }
92
93     lenW = MultiByteToWideChar(cp, 0, str, count, NULL, 0);
94     strW = HeapAlloc(GetProcessHeap(), 0, (lenW + 1) * sizeof(WCHAR));
95     MultiByteToWideChar(cp, 0, str, count, strW, lenW);
96     strW[lenW] = '\0';
97     if(plenW) *plenW = lenW;
98     if(pCP) *pCP = cp;
99     return strW;
100 }
101
102 /***********************************************************************
103  *           ExtTextOut (GDI.351)
104  */
105 BOOL16 WINAPI ExtTextOut16( HDC16 hdc, INT16 x, INT16 y, UINT16 flags,
106                             const RECT16 *lprect, LPCSTR str, UINT16 count,
107                             const INT16 *lpDx )
108 {
109     BOOL        ret;
110     int         i;
111     RECT        rect32;
112     LPINT       lpdx32 = NULL;
113
114     if (lpDx) {
115         lpdx32 = (LPINT)HeapAlloc( GetProcessHeap(),0, sizeof(INT)*count );
116         if(lpdx32 == NULL) return FALSE;
117         for (i=count;i--;) lpdx32[i]=lpDx[i];
118     }
119     if (lprect) CONV_RECT16TO32(lprect,&rect32);
120     ret = ExtTextOutA(hdc,x,y,flags,lprect?&rect32:NULL,str,count,lpdx32);
121     if (lpdx32) HeapFree( GetProcessHeap(), 0, lpdx32 );
122     return ret;
123 }
124
125
126 /***********************************************************************
127  *           ExtTextOutA    (GDI32.@)
128  */
129 BOOL WINAPI ExtTextOutA( HDC hdc, INT x, INT y, UINT flags,
130                          const RECT *lprect, LPCSTR str, UINT count, const INT *lpDx )
131 {
132     INT wlen;
133     UINT codepage;
134     LPWSTR p = FONT_mbtowc(hdc, str, count, &wlen, &codepage);
135     BOOL ret;
136     LPINT lpDxW = NULL;
137
138     if (lpDx) {
139         unsigned int i = 0, j = 0;
140
141         lpDxW = (LPINT)HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(INT));
142         while(i < count) {
143             if(IsDBCSLeadByteEx(codepage, str[i])) {
144                 lpDxW[j++] = lpDx[i] + lpDx[i+1];
145                 i = i + 2;
146             } else {
147                 lpDxW[j++] = lpDx[i];
148                 i = i + 1;
149             }
150         }
151     }
152
153     ret = ExtTextOutW( hdc, x, y, flags, lprect, p, wlen, lpDxW );
154
155     HeapFree( GetProcessHeap(), 0, p );
156     if (lpDxW) HeapFree( GetProcessHeap(), 0, lpDxW );
157     return ret;
158 }
159
160
161 /***********************************************************************
162  *           ExtTextOutW    (GDI32.@)
163  */
164 BOOL WINAPI ExtTextOutW( HDC hdc, INT x, INT y, UINT flags,
165                          const RECT *lprect, LPCWSTR str, UINT count, const INT *lpDx )
166 {
167     BOOL ret = FALSE;
168     DC * dc = DC_GetDCUpdate( hdc );
169     if (dc)
170     {
171         if(PATH_IsPathOpen(dc->path))
172             FIXME("called on an open path\n");
173                 else if(dc->funcs->pExtTextOut)
174                 {
175                         DWORD fontLangInfo=0;
176                         if( !(flags&(ETO_GLYPH_INDEX|ETO_IGNORELANGUAGE)) &&
177                            ((fontLangInfo=GetFontLanguageInfo( hdc ))&(GCP_REORDER|GCP_GLYPHSHAPE)) )
178                         {
179                                 /* The caller did not specify that language processing was already done,
180                                  * and the font idetifies iteself as requiring language processing.
181                                  */
182                                 GCP_RESULTSW gcp;
183                                 
184                                 gcp.lStructSize=sizeof(gcp);
185                                 gcp.lpOutString=HeapAlloc(GetProcessHeap(), 0, count*sizeof(WCHAR));
186                                 gcp.lpOrder=NULL;
187                                 gcp.lpDx=NULL;
188                                 gcp.lpCaretPos=NULL;
189                                 gcp.lpClass=NULL;
190                                 gcp.lpGlyphs=NULL;
191                                 gcp.nGlyphs=0;
192                                 gcp.nMaxFit=0;
193                                 
194                                 GetCharacterPlacementW(hdc, str, count, 0, &gcp, GCP_REORDER );
195                                 
196                                 ret = dc->funcs->pExtTextOut(dc->physDev,x,y,flags|ETO_IGNORELANGUAGE,
197                                         lprect,gcp.lpOutString,count,lpDx);
198                         } else
199                                 ret = dc->funcs->pExtTextOut(dc->physDev,x,y,flags,lprect,str,count,lpDx);
200                 }
201                 GDI_ReleaseObj( hdc );
202     }
203     return ret;
204 }
205
206
207 /***********************************************************************
208  *           TextOut    (GDI.33)
209  */
210 BOOL16 WINAPI TextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR str, INT16 count )
211 {
212     return ExtTextOut16( hdc, x, y, 0, NULL, str, count, NULL );
213 }
214
215
216 /***********************************************************************
217  *           TextOutA    (GDI32.@)
218  */
219 BOOL WINAPI TextOutA( HDC hdc, INT x, INT y, LPCSTR str, INT count )
220 {
221     return ExtTextOutA( hdc, x, y, 0, NULL, str, count, NULL );
222 }
223
224
225 /***********************************************************************
226  *           TextOutW    (GDI32.@)
227  */
228 BOOL WINAPI TextOutW(HDC hdc, INT x, INT y, LPCWSTR str, INT count)
229 {
230     return ExtTextOutW( hdc, x, y, 0, NULL, str, count, NULL );
231 }
232
233
234 /***********************************************************************
235  * GetTextCharset [GDI32.@]  Gets character set for font in DC
236  *
237  * NOTES
238  *    Should it return a UINT32 instead of an INT32?
239  *    => YES, as GetTextCharsetInfo returns UINT32
240  *
241  * RETURNS
242  *    Success: Character set identifier
243  *    Failure: DEFAULT_CHARSET
244  */
245 UINT WINAPI GetTextCharset(
246     HDC hdc) /* [in] Handle to device context */
247 {
248     /* MSDN docs say this is equivalent */
249     return GetTextCharsetInfo(hdc, NULL, 0);
250 }
251
252 /***********************************************************************
253  * GetTextCharset [GDI.612]
254  */
255 UINT16 WINAPI GetTextCharset16(HDC16 hdc)
256 {
257     return (UINT16)GetTextCharset(hdc);
258 }
259
260 /***********************************************************************
261  * GetTextCharsetInfo [GDI32.@]  Gets character set for font
262  *
263  * NOTES
264  *    Should csi be an LPFONTSIGNATURE instead of an LPCHARSETINFO?
265  *    Should it return a UINT32 instead of an INT32?
266  *    => YES and YES, from win32.hlp from Borland
267  *
268  *    This returns the actual charset selected by the driver rather than the
269  *    value in lf.lfCharSet during CreateFont, to get that use
270  *    GetObject(GetCurrentObject(...),...)
271  *
272  * RETURNS
273  *    Success: Character set identifier
274  *    Failure: DEFAULT_CHARSET
275  */
276 UINT WINAPI GetTextCharsetInfo(
277     HDC hdc,            /* [in]  Handle to device context */
278     LPFONTSIGNATURE fs, /* [out] Pointer to struct to receive data */
279     DWORD flags)        /* [in]  Reserved - must be 0 */
280 {
281     UINT charSet = DEFAULT_CHARSET;
282     CHARSETINFO csinfo;
283     TEXTMETRICW tm;
284
285     if(!GetTextMetricsW(hdc, &tm)) return DEFAULT_CHARSET;
286     charSet = tm.tmCharSet;
287
288     if (fs != NULL) {
289       if (!TranslateCharsetInfo((LPDWORD)charSet, &csinfo, TCI_SRCCHARSET))
290            return DEFAULT_CHARSET;
291       memcpy(fs, &csinfo.fs, sizeof(FONTSIGNATURE));
292     }
293     return charSet;
294 }
295
296 /***********************************************************************
297  *              PolyTextOutA (GDI32.@)
298  *
299  * Draw several Strings
300  */
301 BOOL WINAPI PolyTextOutA (
302                           HDC hdc,               /* [in] Handle to device context */
303                           PPOLYTEXTA pptxt,      /* [in] Array of strings */
304                           INT cStrings           /* [in] Number of strings in array */
305                           )
306 {
307   FIXME("stub!\n");
308   SetLastError ( ERROR_CALL_NOT_IMPLEMENTED );
309   return 0;
310 }
311
312
313
314 /***********************************************************************
315  *              PolyTextOutW (GDI32.@)
316  *
317  * Draw several Strings
318  */
319 BOOL WINAPI PolyTextOutW (
320                           HDC hdc,               /* [in] Handle to device context */
321                           PPOLYTEXTW pptxt,      /* [in] Array of strings */
322                           INT cStrings           /* [in] Number of strings in array */
323                           )
324 {
325   FIXME("stub!\n");
326   SetLastError ( ERROR_CALL_NOT_IMPLEMENTED );
327   return 0;
328 }