use DEFAULT_CHARSET instead of ANSI_CHARSET
[wxWidgets.git] / src / msw / fontutil.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/fontutil.cpp
3 // Purpose: font-related helper functions for wxMSW
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 05.11.99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "fontutil.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/string.h"
33 #include "wx/log.h"
34 #include "wx/intl.h"
35 #endif //WX_PRECOMP
36
37 #include "wx/msw/private.h"
38
39 #include "wx/fontutil.h"
40 #include "wx/fontmap.h"
41
42 #include "wx/tokenzr.h"
43
44 // for MSVC5 and old w32api
45 #ifndef HANGUL_CHARSET
46 # define HANGUL_CHARSET 129
47 #endif
48
49 // ============================================================================
50 // implementation
51 // ============================================================================
52
53 // ----------------------------------------------------------------------------
54 // wxNativeEncodingInfo
55 // ----------------------------------------------------------------------------
56
57 // convert to/from the string representation: format is
58 // encodingid;facename[;charset]
59
60 bool wxNativeEncodingInfo::FromString(const wxString& s)
61 {
62 wxStringTokenizer tokenizer(s, _T(";"));
63
64 wxString encid = tokenizer.GetNextToken();
65 long enc;
66 if ( !encid.ToLong(&enc) )
67 return FALSE;
68 encoding = (wxFontEncoding)enc;
69
70 facename = tokenizer.GetNextToken();
71
72 wxString tmp = tokenizer.GetNextToken();
73 if ( tmp.empty() )
74 {
75 // default charset: but don't use DEFAULT_CHARSET here because it might
76 // be different from the machine on which the file we had read this
77 // encoding desc from was created
78 charset = ANSI_CHARSET;
79 }
80 else
81 {
82 if ( wxSscanf(tmp, _T("%u"), &charset) != 1 )
83 {
84 // should be a number!
85 return FALSE;
86 }
87 }
88
89 return TRUE;
90 }
91
92 wxString wxNativeEncodingInfo::ToString() const
93 {
94 wxString s;
95
96 s << (long)encoding << _T(';') << facename;
97
98 // ANSI_CHARSET is assumed anyhow
99 if ( charset != ANSI_CHARSET )
100 {
101 s << _T(';') << charset;
102 }
103
104 return s;
105 }
106
107 // ----------------------------------------------------------------------------
108 // helper functions
109 // ----------------------------------------------------------------------------
110
111 bool wxGetNativeFontEncoding(wxFontEncoding encoding,
112 wxNativeEncodingInfo *info)
113 {
114 wxCHECK_MSG( info, FALSE, _T("bad pointer in wxGetNativeFontEncoding") );
115
116 if ( encoding == wxFONTENCODING_DEFAULT )
117 {
118 encoding = wxFont::GetDefaultEncoding();
119 }
120
121 switch ( encoding )
122 {
123 // although this function is supposed to return an exact match, do do
124 // some mappings here for the most common case of "standard" encoding
125 case wxFONTENCODING_SYSTEM:
126 info->charset = DEFAULT_CHARSET;
127 break;
128
129 case wxFONTENCODING_ISO8859_1:
130 case wxFONTENCODING_ISO8859_15:
131 case wxFONTENCODING_CP1252:
132 info->charset = ANSI_CHARSET;
133 break;
134
135 #if !defined(__WIN16__) && !defined(__WXMICROWIN__)
136
137 // The following four fonts are multi-byte charsets
138 case wxFONTENCODING_CP932:
139 info->charset = SHIFTJIS_CHARSET;
140 break;
141
142 case wxFONTENCODING_CP936:
143 info->charset = GB2312_CHARSET;
144 break;
145
146 case wxFONTENCODING_CP949:
147 info->charset = HANGUL_CHARSET;
148 break;
149
150 case wxFONTENCODING_CP950:
151 info->charset = CHINESEBIG5_CHARSET;
152 break;
153
154 // The rest are single byte encodings
155 case wxFONTENCODING_CP1250:
156 info->charset = EASTEUROPE_CHARSET;
157 break;
158
159 case wxFONTENCODING_CP1251:
160 info->charset = RUSSIAN_CHARSET;
161 break;
162
163 case wxFONTENCODING_CP1253:
164 info->charset = GREEK_CHARSET;
165 break;
166
167 case wxFONTENCODING_CP1254:
168 info->charset = TURKISH_CHARSET;
169 break;
170
171 case wxFONTENCODING_CP1255:
172 info->charset = HEBREW_CHARSET;
173 break;
174
175 case wxFONTENCODING_CP1256:
176 info->charset = ARABIC_CHARSET;
177 break;
178
179 case wxFONTENCODING_CP1257:
180 info->charset = BALTIC_CHARSET;
181 break;
182
183 case wxFONTENCODING_CP874:
184 info->charset = THAI_CHARSET;
185 break;
186
187
188 #endif // !Win16
189
190 case wxFONTENCODING_CP437:
191 info->charset = OEM_CHARSET;
192 break;
193
194 default:
195 // no way to translate this encoding into a Windows charset
196 return FALSE;
197 }
198
199 info->encoding = encoding;
200
201 return TRUE;
202 }
203
204 bool wxTestFontEncoding(const wxNativeEncodingInfo& info)
205 {
206 // try to create such font
207 LOGFONT lf;
208 wxZeroMemory(lf); // all default values
209
210 lf.lfCharSet = info.charset;
211 wxStrncpy(lf.lfFaceName, info.facename, WXSIZEOF(lf.lfFaceName));
212
213 HFONT hfont = ::CreateFontIndirect(&lf);
214 if ( !hfont )
215 {
216 // no such font
217 return FALSE;
218 }
219
220 ::DeleteObject((HGDIOBJ)hfont);
221
222 return TRUE;
223 }
224
225 // ----------------------------------------------------------------------------
226 // wxFontEncoding <-> CHARSET_XXX
227 // ----------------------------------------------------------------------------
228
229 wxFontEncoding wxGetFontEncFromCharSet(int cs)
230 {
231 wxFontEncoding fontEncoding;
232
233 switch ( cs )
234 {
235 default:
236 // assume the system charset
237 fontEncoding = wxFONTENCODING_SYSTEM;
238 break;
239
240 case ANSI_CHARSET:
241 fontEncoding = wxFONTENCODING_CP1252;
242 break;
243
244 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
245 case EASTEUROPE_CHARSET:
246 fontEncoding = wxFONTENCODING_CP1250;
247 break;
248
249 case BALTIC_CHARSET:
250 fontEncoding = wxFONTENCODING_CP1257;
251 break;
252
253 case RUSSIAN_CHARSET:
254 fontEncoding = wxFONTENCODING_CP1251;
255 break;
256
257 case ARABIC_CHARSET:
258 fontEncoding = wxFONTENCODING_CP1256;
259 break;
260
261 case GREEK_CHARSET:
262 fontEncoding = wxFONTENCODING_CP1253;
263 break;
264
265 case HEBREW_CHARSET:
266 fontEncoding = wxFONTENCODING_CP1255;
267 break;
268
269 case TURKISH_CHARSET:
270 fontEncoding = wxFONTENCODING_CP1254;
271 break;
272
273 case THAI_CHARSET:
274 fontEncoding = wxFONTENCODING_CP437;
275 break;
276
277 case SHIFTJIS_CHARSET:
278 fontEncoding = wxFONTENCODING_CP932;
279 break;
280
281 case GB2312_CHARSET:
282 fontEncoding = wxFONTENCODING_CP936;
283 break;
284
285 case HANGUL_CHARSET:
286 fontEncoding = wxFONTENCODING_CP949;
287 break;
288
289 case CHINESEBIG5_CHARSET:
290 fontEncoding = wxFONTENCODING_CP950;
291 break;
292
293 #endif // Win32
294
295 case OEM_CHARSET:
296 fontEncoding = wxFONTENCODING_CP437;
297 break;
298 }
299
300 return fontEncoding;
301 }
302
303 // ----------------------------------------------------------------------------
304 // wxFont <-> LOGFONT conversion
305 // ----------------------------------------------------------------------------
306
307 void wxFillLogFont(LOGFONT *logFont, const wxFont *font)
308 {
309 // maybe we already have LOGFONT for this font?
310 wxNativeFontInfo *fontinfo = font->GetNativeFontInfo();
311 if ( !fontinfo )
312 {
313 // use wxNativeFontInfo methods to build a LOGFONT for this font
314 fontinfo = new wxNativeFontInfo;
315
316 // translate all font parameters
317 fontinfo->SetStyle((wxFontStyle)font->GetStyle());
318 fontinfo->SetWeight((wxFontWeight)font->GetWeight());
319 fontinfo->SetUnderlined(font->GetUnderlined());
320 fontinfo->SetPointSize(font->GetPointSize());
321
322 // set the family/facename
323 fontinfo->SetFamily((wxFontFamily)font->GetFamily());
324 wxString facename = font->GetFaceName();
325 if ( !facename.empty() )
326 {
327 fontinfo->SetFaceName(facename);
328 }
329
330 // deal with encoding now (it may override the font family and facename
331 // so do it after setting them)
332 fontinfo->SetEncoding(font->GetEncoding());
333 }
334
335 // transfer all the data to LOGFONT
336 *logFont = fontinfo->lf;
337
338 delete fontinfo;
339 }
340
341 wxFont wxCreateFontFromLogFont(const LOGFONT *logFont)
342 {
343 wxNativeFontInfo info;
344
345 info.lf = *logFont;
346
347 return wxFont(info);
348 }
349