pointer returned by GetNativeFontInfo() is now const and must not be deleted (replace...
[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 long enc;
67 if ( !encid.ToLong(&enc) )
68 return FALSE;
69 encoding = (wxFontEncoding)enc;
70
71 facename = tokenizer.GetNextToken();
72
73 wxString tmp = tokenizer.GetNextToken();
74 if ( tmp.empty() )
75 {
76 // default charset: but don't use DEFAULT_CHARSET here because it might
77 // be different from the machine on which the file we had read this
78 // encoding desc from was created
79 charset = ANSI_CHARSET;
80 }
81 else
82 {
83 if ( wxSscanf(tmp, _T("%u"), &charset) != 1 )
84 {
85 // should be a number!
86 return FALSE;
87 }
88 }
89
90 return TRUE;
91 }
92
93 wxString wxNativeEncodingInfo::ToString() const
94 {
95 wxString s;
96
97 s << (long)encoding << _T(';') << facename;
98
99 // ANSI_CHARSET is assumed anyhow
100 if ( charset != ANSI_CHARSET )
101 {
102 s << _T(';') << charset;
103 }
104
105 return s;
106 }
107
108 // ----------------------------------------------------------------------------
109 // helper functions
110 // ----------------------------------------------------------------------------
111
112 bool wxGetNativeFontEncoding(wxFontEncoding encoding,
113 wxNativeEncodingInfo *info)
114 {
115 wxCHECK_MSG( info, FALSE, _T("bad pointer in wxGetNativeFontEncoding") );
116
117 if ( encoding == wxFONTENCODING_DEFAULT )
118 {
119 encoding = wxFont::GetDefaultEncoding();
120 }
121
122 extern WXDLLIMPEXP_BASE long wxEncodingToCharset(wxFontEncoding encoding);
123 info->charset = wxEncodingToCharset(encoding);
124 if ( info->charset == -1 )
125 return FALSE;
126
127 info->encoding = encoding;
128
129 return TRUE;
130 }
131
132 bool wxTestFontEncoding(const wxNativeEncodingInfo& info)
133 {
134 // try to create such font
135 LOGFONT lf;
136 wxZeroMemory(lf); // all default values
137
138 lf.lfCharSet = info.charset;
139 wxStrncpy(lf.lfFaceName, info.facename, WXSIZEOF(lf.lfFaceName));
140
141 HFONT hfont = ::CreateFontIndirect(&lf);
142 if ( !hfont )
143 {
144 // no such font
145 return FALSE;
146 }
147
148 ::DeleteObject((HGDIOBJ)hfont);
149
150 return TRUE;
151 }
152
153 // ----------------------------------------------------------------------------
154 // wxFontEncoding <-> CHARSET_XXX
155 // ----------------------------------------------------------------------------
156
157 wxFontEncoding wxGetFontEncFromCharSet(int cs)
158 {
159 wxFontEncoding fontEncoding;
160
161 switch ( cs )
162 {
163 default:
164 // assume the system charset
165 fontEncoding = wxFONTENCODING_SYSTEM;
166 break;
167
168 case ANSI_CHARSET:
169 fontEncoding = wxFONTENCODING_CP1252;
170 break;
171
172 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
173 case EASTEUROPE_CHARSET:
174 fontEncoding = wxFONTENCODING_CP1250;
175 break;
176
177 case BALTIC_CHARSET:
178 fontEncoding = wxFONTENCODING_CP1257;
179 break;
180
181 case RUSSIAN_CHARSET:
182 fontEncoding = wxFONTENCODING_CP1251;
183 break;
184
185 case ARABIC_CHARSET:
186 fontEncoding = wxFONTENCODING_CP1256;
187 break;
188
189 case GREEK_CHARSET:
190 fontEncoding = wxFONTENCODING_CP1253;
191 break;
192
193 case HEBREW_CHARSET:
194 fontEncoding = wxFONTENCODING_CP1255;
195 break;
196
197 case TURKISH_CHARSET:
198 fontEncoding = wxFONTENCODING_CP1254;
199 break;
200
201 case THAI_CHARSET:
202 fontEncoding = wxFONTENCODING_CP437;
203 break;
204
205 case SHIFTJIS_CHARSET:
206 fontEncoding = wxFONTENCODING_CP932;
207 break;
208
209 case GB2312_CHARSET:
210 fontEncoding = wxFONTENCODING_CP936;
211 break;
212
213 case HANGUL_CHARSET:
214 fontEncoding = wxFONTENCODING_CP949;
215 break;
216
217 case CHINESEBIG5_CHARSET:
218 fontEncoding = wxFONTENCODING_CP950;
219 break;
220
221 #endif // Win32
222
223 case OEM_CHARSET:
224 fontEncoding = wxFONTENCODING_CP437;
225 break;
226 }
227
228 return fontEncoding;
229 }
230
231 // ----------------------------------------------------------------------------
232 // wxFont <-> LOGFONT conversion
233 // ----------------------------------------------------------------------------
234
235 void wxFillLogFont(LOGFONT *logFont, const wxFont *font)
236 {
237 wxNativeFontInfo fi;
238
239 // maybe we already have LOGFONT for this font?
240 const wxNativeFontInfo *pFI = font->GetNativeFontInfo();
241 if ( !pFI )
242 {
243 // use wxNativeFontInfo methods to build a LOGFONT for this font
244 fi.InitFromFont(*font);
245
246 pFI = &fi;
247 }
248
249 // transfer all the data to LOGFONT
250 *logFont = pFI->lf;
251 }
252
253 wxFont wxCreateFontFromLogFont(const LOGFONT *logFont)
254 {
255 wxNativeFontInfo info;
256
257 info.lf = *logFont;
258
259 return wxFont(info);
260 }
261