]> git.saurik.com Git - wxWidgets.git/blame - src/msw/fontutil.cpp
Fix memory leak by letting the base class version handle the
[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>
77ffb593 9// Licence: wxWidgets licence
11c7d5b6
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11c7d5b6
VZ
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"
e4ffab29 35 #include "wx/encinfo.h"
11c7d5b6
VZ
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
e757496e
MB
45// for MSVC5 and old w32api
46#ifndef HANGUL_CHARSET
47# define HANGUL_CHARSET 129
48#endif
49
11c7d5b6
VZ
50// ============================================================================
51// implementation
52// ============================================================================
53
54// ----------------------------------------------------------------------------
55// wxNativeEncodingInfo
56// ----------------------------------------------------------------------------
57
58// convert to/from the string representation: format is
1e1d0be1 59// encodingid;facename[;charset]
11c7d5b6
VZ
60
61bool wxNativeEncodingInfo::FromString(const wxString& s)
62{
63 wxStringTokenizer tokenizer(s, _T(";"));
64
1e1d0be1 65 wxString encid = tokenizer.GetNextToken();
24056b9e
VZ
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
1e1d0be1 69 long enc;
24056b9e
VZ
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 }
1e1d0be1 89
11c7d5b6 90 facename = tokenizer.GetNextToken();
11c7d5b6
VZ
91
92 wxString tmp = tokenizer.GetNextToken();
4ec6efd6 93 if ( tmp.empty() )
11c7d5b6 94 {
4ec6efd6
VZ
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
11c7d5b6
VZ
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
112wxString wxNativeEncodingInfo::ToString() const
113{
1e1d0be1 114 wxString s;
f6bcfd97 115
24056b9e
VZ
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;
4ec6efd6
VZ
127
128 // ANSI_CHARSET is assumed anyhow
11c7d5b6
VZ
129 if ( charset != ANSI_CHARSET )
130 {
1e1d0be1 131 s << _T(';') << charset;
11c7d5b6
VZ
132 }
133
134 return s;
135}
136
137// ----------------------------------------------------------------------------
138// helper functions
139// ----------------------------------------------------------------------------
140
141bool 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
bddd7a8d 151 extern WXDLLIMPEXP_BASE long wxEncodingToCharset(wxFontEncoding encoding);
e2478fde
VZ
152 info->charset = wxEncodingToCharset(encoding);
153 if ( info->charset == -1 )
154 return FALSE;
11c7d5b6 155
6b0eebc5 156 info->encoding = encoding;
f6bcfd97 157
11c7d5b6
VZ
158 return TRUE;
159}
160
161bool 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;
98a52819 168 wxStrncpy(lf.lfFaceName, info.facename, WXSIZEOF(lf.lfFaceName));
11c7d5b6
VZ
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
f6bcfd97
BP
182// ----------------------------------------------------------------------------
183// wxFontEncoding <-> CHARSET_XXX
184// ----------------------------------------------------------------------------
185
186wxFontEncoding wxGetFontEncFromCharSet(int cs)
187{
188 wxFontEncoding fontEncoding;
189
190 switch ( cs )
191 {
192 default:
b19dae2c
VZ
193 wxFAIL_MSG( _T("unexpected Win32 charset") );
194 // fall through and assume the system charset
195
196 case DEFAULT_CHARSET:
4ec6efd6
VZ
197 fontEncoding = wxFONTENCODING_SYSTEM;
198 break;
f6bcfd97
BP
199
200 case ANSI_CHARSET:
201 fontEncoding = wxFONTENCODING_CP1252;
202 break;
203
b19dae2c
VZ
204 case SYMBOL_CHARSET:
205 // what can we do here?
206 fontEncoding = wxFONTENCODING_MAX;
207 break;
208
04ef50df 209#if defined(__WIN32__) && !defined(__WXMICROWIN__)
f6bcfd97
BP
210 case EASTEUROPE_CHARSET:
211 fontEncoding = wxFONTENCODING_CP1250;
212 break;
213
214 case BALTIC_CHARSET:
215 fontEncoding = wxFONTENCODING_CP1257;
216 break;
217
218 case RUSSIAN_CHARSET:
219 fontEncoding = wxFONTENCODING_CP1251;
220 break;
221
222 case ARABIC_CHARSET:
223 fontEncoding = wxFONTENCODING_CP1256;
224 break;
225
226 case GREEK_CHARSET:
227 fontEncoding = wxFONTENCODING_CP1253;
228 break;
229
230 case HEBREW_CHARSET:
231 fontEncoding = wxFONTENCODING_CP1255;
232 break;
233
234 case TURKISH_CHARSET:
235 fontEncoding = wxFONTENCODING_CP1254;
236 break;
237
238 case THAI_CHARSET:
239 fontEncoding = wxFONTENCODING_CP437;
240 break;
bc4e6fcd
VZ
241
242 case SHIFTJIS_CHARSET:
243 fontEncoding = wxFONTENCODING_CP932;
244 break;
245
246 case GB2312_CHARSET:
247 fontEncoding = wxFONTENCODING_CP936;
248 break;
249
250 case HANGUL_CHARSET:
251 fontEncoding = wxFONTENCODING_CP949;
252 break;
253
254 case CHINESEBIG5_CHARSET:
255 fontEncoding = wxFONTENCODING_CP950;
256 break;
257
f6bcfd97
BP
258#endif // Win32
259
260 case OEM_CHARSET:
261 fontEncoding = wxFONTENCODING_CP437;
262 break;
263 }
264
265 return fontEncoding;
266}
267
11c7d5b6
VZ
268// ----------------------------------------------------------------------------
269// wxFont <-> LOGFONT conversion
270// ----------------------------------------------------------------------------
271
272void wxFillLogFont(LOGFONT *logFont, const wxFont *font)
273{
3bf5a59b
VZ
274 wxNativeFontInfo fi;
275
7936354d 276 // maybe we already have LOGFONT for this font?
3bf5a59b
VZ
277 const wxNativeFontInfo *pFI = font->GetNativeFontInfo();
278 if ( !pFI )
11c7d5b6 279 {
7936354d 280 // use wxNativeFontInfo methods to build a LOGFONT for this font
3bf5a59b 281 fi.InitFromFont(*font);
11c7d5b6 282
3bf5a59b 283 pFI = &fi;
11c7d5b6
VZ
284 }
285
286 // transfer all the data to LOGFONT
3bf5a59b 287 *logFont = pFI->lf;
11c7d5b6
VZ
288}
289
290wxFont wxCreateFontFromLogFont(const LOGFONT *logFont)
291{
09fcd889 292 wxNativeFontInfo info;
11c7d5b6 293
09fcd889 294 info.lf = *logFont;
11c7d5b6 295
09fcd889 296 return wxFont(info);
11c7d5b6
VZ
297}
298