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