]> git.saurik.com Git - wxWidgets.git/blame - src/msw/fontutil.cpp
Add missing t_str() call to fix wxUSE_STL build after r73792.
[wxWidgets.git] / src / msw / fontutil.cpp
CommitLineData
11c7d5b6 1///////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/msw/fontutil.cpp
11c7d5b6
VZ
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
081d8d96
PC
27#include "wx/fontutil.h"
28
11c7d5b6
VZ
29#ifndef WX_PRECOMP
30 #include "wx/string.h"
31 #include "wx/log.h"
32 #include "wx/intl.h"
193d0c93 33 #include "wx/wxcrtvararg.h"
081d8d96 34 #include "wx/msw/private.h"
11c7d5b6
VZ
35#endif //WX_PRECOMP
36
081d8d96 37#include "wx/encinfo.h"
11c7d5b6 38#include "wx/fontmap.h"
11c7d5b6
VZ
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{
9a83f860 59 wxStringTokenizer tokenizer(s, wxT(";"));
11c7d5b6 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 {
9a83f860 98 if ( wxSscanf(tmp, wxT("%u"), &charset) != 1 )
11c7d5b6
VZ
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
9a83f860 122 << wxT(';') << facename;
4ec6efd6
VZ
123
124 // ANSI_CHARSET is assumed anyhow
11c7d5b6
VZ
125 if ( charset != ANSI_CHARSET )
126 {
9a83f860 127 s << wxT(';') << charset;
11c7d5b6
VZ
128 }
129
130 return s;
131}
132
133// ----------------------------------------------------------------------------
134// helper functions
135// ----------------------------------------------------------------------------
136
137bool wxGetNativeFontEncoding(wxFontEncoding encoding,
138 wxNativeEncodingInfo *info)
139{
9a83f860 140 wxCHECK_MSG( info, false, wxT("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;
64accea5 164 wxStrlcpy(lf.lfFaceName, info.facename.c_str(), 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:
9a83f860 189 wxFAIL_MSG( wxT("unexpected Win32 charset") );
b19dae2c
VZ
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
2253f181
VZ
254 case VIETNAMESE_CHARSET:
255 fontEncoding = wxFONTENCODING_CP1258;
256 break;
257
258 case JOHAB_CHARSET:
259 fontEncoding = wxFONTENCODING_CP1361;
260 break;
261
f6bcfd97
BP
262#endif // Win32
263
264 case OEM_CHARSET:
265 fontEncoding = wxFONTENCODING_CP437;
266 break;
267 }
268
269 return fontEncoding;
270}
271
11c7d5b6
VZ
272// ----------------------------------------------------------------------------
273// wxFont <-> LOGFONT conversion
274// ----------------------------------------------------------------------------
275
276void wxFillLogFont(LOGFONT *logFont, const wxFont *font)
277{
3bf5a59b
VZ
278 wxNativeFontInfo fi;
279
7936354d 280 // maybe we already have LOGFONT for this font?
3bf5a59b
VZ
281 const wxNativeFontInfo *pFI = font->GetNativeFontInfo();
282 if ( !pFI )
11c7d5b6 283 {
7936354d 284 // use wxNativeFontInfo methods to build a LOGFONT for this font
3bf5a59b 285 fi.InitFromFont(*font);
11c7d5b6 286
3bf5a59b 287 pFI = &fi;
11c7d5b6
VZ
288 }
289
290 // transfer all the data to LOGFONT
3bf5a59b 291 *logFont = pFI->lf;
11c7d5b6
VZ
292}
293
294wxFont wxCreateFontFromLogFont(const LOGFONT *logFont)
295{
09fcd889 296 wxNativeFontInfo info;
11c7d5b6 297
09fcd889 298 info.lf = *logFont;
11c7d5b6 299
09fcd889 300 return wxFont(info);
11c7d5b6
VZ
301}
302