serialize wxNativeEncodingInfo using font names, not numbers which are subject to...
[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 licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 #include "wx/encinfo.h"
36 #endif //WX_PRECOMP
37
38 #include "wx/msw/private.h"
39
40 #include "wx/fontutil.h"
41 #include "wx/fontmap.h"
42
43 #include "wx/tokenzr.h"
44
45 // for MSVC5 and old w32api
46 #ifndef HANGUL_CHARSET
47 # define HANGUL_CHARSET 129
48 #endif
49
50 // ============================================================================
51 // implementation
52 // ============================================================================
53
54 // ----------------------------------------------------------------------------
55 // wxNativeEncodingInfo
56 // ----------------------------------------------------------------------------
57
58 // convert to/from the string representation: format is
59 // encodingid;facename[;charset]
60
61 bool wxNativeEncodingInfo::FromString(const wxString& s)
62 {
63 wxStringTokenizer tokenizer(s, _T(";"));
64
65 wxString encid = tokenizer.GetNextToken();
66
67 // we support 2 formats: the old one (and still used if !wxUSE_FONTMAP)
68 // used the raw encoding values but the new one uses the encoding names
69 long enc;
70 if ( encid.ToLong(&enc) )
71 {
72 // old format, intepret as encoding -- but after minimal checks
73 if ( enc < 0 || enc >= wxFONTENCODING_MAX )
74 return false;
75
76 encoding = (wxFontEncoding)enc;
77 }
78 else // not a number, interpret as an encoding name
79 {
80 #if wxUSE_FONTMAP
81 encoding = wxFontMapper::GetEncodingFromName(encid);
82 if ( encoding == wxFONTENCODING_MAX )
83 #endif // wxUSE_FONTMAP
84 {
85 // failed to parse the name (or couldn't even try...)
86 return false;
87 }
88 }
89
90 facename = tokenizer.GetNextToken();
91
92 wxString tmp = tokenizer.GetNextToken();
93 if ( tmp.empty() )
94 {
95 // default charset: but don't use DEFAULT_CHARSET here because it might
96 // be different from the machine on which the file we had read this
97 // encoding desc from was created
98 charset = ANSI_CHARSET;
99 }
100 else
101 {
102 if ( wxSscanf(tmp, _T("%u"), &charset) != 1 )
103 {
104 // should be a number!
105 return FALSE;
106 }
107 }
108
109 return TRUE;
110 }
111
112 wxString wxNativeEncodingInfo::ToString() const
113 {
114 wxString s;
115
116 s
117 #if wxUSE_FONTMAP
118 // use the encoding names as this is safer than using the numerical
119 // values which may change with time (because new encodings are
120 // inserted...)
121 << wxFontMapper::GetEncodingName(encoding)
122 #else // !wxUSE_FONTMAP
123 // we don't have any choice but to use the raw value
124 << (long)encoding
125 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
126 << _T(';') << facename;
127
128 // ANSI_CHARSET is assumed anyhow
129 if ( charset != ANSI_CHARSET )
130 {
131 s << _T(';') << charset;
132 }
133
134 return s;
135 }
136
137 // ----------------------------------------------------------------------------
138 // helper functions
139 // ----------------------------------------------------------------------------
140
141 bool wxGetNativeFontEncoding(wxFontEncoding encoding,
142 wxNativeEncodingInfo *info)
143 {
144 wxCHECK_MSG( info, FALSE, _T("bad pointer in wxGetNativeFontEncoding") );
145
146 if ( encoding == wxFONTENCODING_DEFAULT )
147 {
148 encoding = wxFont::GetDefaultEncoding();
149 }
150
151 extern WXDLLIMPEXP_BASE long wxEncodingToCharset(wxFontEncoding encoding);
152 info->charset = wxEncodingToCharset(encoding);
153 if ( info->charset == -1 )
154 return FALSE;
155
156 info->encoding = encoding;
157
158 return TRUE;
159 }
160
161 bool wxTestFontEncoding(const wxNativeEncodingInfo& info)
162 {
163 // try to create such font
164 LOGFONT lf;
165 wxZeroMemory(lf); // all default values
166
167 lf.lfCharSet = info.charset;
168 wxStrncpy(lf.lfFaceName, info.facename, WXSIZEOF(lf.lfFaceName));
169
170 HFONT hfont = ::CreateFontIndirect(&lf);
171 if ( !hfont )
172 {
173 // no such font
174 return FALSE;
175 }
176
177 ::DeleteObject((HGDIOBJ)hfont);
178
179 return TRUE;
180 }
181
182 // ----------------------------------------------------------------------------
183 // wxFontEncoding <-> CHARSET_XXX
184 // ----------------------------------------------------------------------------
185
186 wxFontEncoding wxGetFontEncFromCharSet(int cs)
187 {
188 wxFontEncoding fontEncoding;
189
190 switch ( cs )
191 {
192 default:
193 // assume the system charset
194 fontEncoding = wxFONTENCODING_SYSTEM;
195 break;
196
197 case ANSI_CHARSET:
198 fontEncoding = wxFONTENCODING_CP1252;
199 break;
200
201 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
202 case EASTEUROPE_CHARSET:
203 fontEncoding = wxFONTENCODING_CP1250;
204 break;
205
206 case BALTIC_CHARSET:
207 fontEncoding = wxFONTENCODING_CP1257;
208 break;
209
210 case RUSSIAN_CHARSET:
211 fontEncoding = wxFONTENCODING_CP1251;
212 break;
213
214 case ARABIC_CHARSET:
215 fontEncoding = wxFONTENCODING_CP1256;
216 break;
217
218 case GREEK_CHARSET:
219 fontEncoding = wxFONTENCODING_CP1253;
220 break;
221
222 case HEBREW_CHARSET:
223 fontEncoding = wxFONTENCODING_CP1255;
224 break;
225
226 case TURKISH_CHARSET:
227 fontEncoding = wxFONTENCODING_CP1254;
228 break;
229
230 case THAI_CHARSET:
231 fontEncoding = wxFONTENCODING_CP437;
232 break;
233
234 case SHIFTJIS_CHARSET:
235 fontEncoding = wxFONTENCODING_CP932;
236 break;
237
238 case GB2312_CHARSET:
239 fontEncoding = wxFONTENCODING_CP936;
240 break;
241
242 case HANGUL_CHARSET:
243 fontEncoding = wxFONTENCODING_CP949;
244 break;
245
246 case CHINESEBIG5_CHARSET:
247 fontEncoding = wxFONTENCODING_CP950;
248 break;
249
250 #endif // Win32
251
252 case OEM_CHARSET:
253 fontEncoding = wxFONTENCODING_CP437;
254 break;
255 }
256
257 return fontEncoding;
258 }
259
260 // ----------------------------------------------------------------------------
261 // wxFont <-> LOGFONT conversion
262 // ----------------------------------------------------------------------------
263
264 void wxFillLogFont(LOGFONT *logFont, const wxFont *font)
265 {
266 wxNativeFontInfo fi;
267
268 // maybe we already have LOGFONT for this font?
269 const wxNativeFontInfo *pFI = font->GetNativeFontInfo();
270 if ( !pFI )
271 {
272 // use wxNativeFontInfo methods to build a LOGFONT for this font
273 fi.InitFromFont(*font);
274
275 pFI = &fi;
276 }
277
278 // transfer all the data to LOGFONT
279 *logFont = pFI->lf;
280 }
281
282 wxFont wxCreateFontFromLogFont(const LOGFONT *logFont)
283 {
284 wxNativeFontInfo info;
285
286 info.lf = *logFont;
287
288 return wxFont(info);
289 }
290