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