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