]> git.saurik.com Git - wxWidgets.git/blob - src/msw/fontutil.cpp
Late note about deprecated wxDbTable constructor.
[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
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
69 long enc;
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 }
89
90 facename = tokenizer.GetNextToken();
91
92 wxString tmp = tokenizer.GetNextToken();
93 if ( tmp.empty() )
94 {
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
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
112 wxString wxNativeEncodingInfo::ToString() const
113 {
114 wxString s;
115
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;
127
128 // ANSI_CHARSET is assumed anyhow
129 if ( charset != ANSI_CHARSET )
130 {
131 s << _T(';') << charset;
132 }
133
134 return s;
135 }
136
137 // ----------------------------------------------------------------------------
138 // helper functions
139 // ----------------------------------------------------------------------------
140
141 bool 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
151 extern WXDLLIMPEXP_BASE long wxEncodingToCharset(wxFontEncoding encoding);
152 info->charset = wxEncodingToCharset(encoding);
153 if ( info->charset == -1 )
154 return false;
155
156 info->encoding = encoding;
157
158 return true;
159 }
160
161 bool 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;
168 wxStrncpy(lf.lfFaceName, info.facename, WXSIZEOF(lf.lfFaceName));
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
182 // ----------------------------------------------------------------------------
183 // wxFontEncoding <-> CHARSET_XXX
184 // ----------------------------------------------------------------------------
185
186 wxFontEncoding wxGetFontEncFromCharSet(int cs)
187 {
188 wxFontEncoding fontEncoding;
189
190 switch ( cs )
191 {
192 default:
193 wxFAIL_MSG( _T("unexpected Win32 charset") );
194 // fall through and assume the system charset
195
196 case DEFAULT_CHARSET:
197 fontEncoding = wxFONTENCODING_SYSTEM;
198 break;
199
200 case ANSI_CHARSET:
201 fontEncoding = wxFONTENCODING_CP1252;
202 break;
203
204 case SYMBOL_CHARSET:
205 // what can we do here?
206 fontEncoding = wxFONTENCODING_MAX;
207 break;
208
209 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
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;
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
258 #endif // Win32
259
260 case OEM_CHARSET:
261 fontEncoding = wxFONTENCODING_CP437;
262 break;
263 }
264
265 return fontEncoding;
266 }
267
268 // ----------------------------------------------------------------------------
269 // wxFont <-> LOGFONT conversion
270 // ----------------------------------------------------------------------------
271
272 void wxFillLogFont(LOGFONT *logFont, const wxFont *font)
273 {
274 wxNativeFontInfo fi;
275
276 // maybe we already have LOGFONT for this font?
277 const wxNativeFontInfo *pFI = font->GetNativeFontInfo();
278 if ( !pFI )
279 {
280 // use wxNativeFontInfo methods to build a LOGFONT for this font
281 fi.InitFromFont(*font);
282
283 pFI = &fi;
284 }
285
286 // transfer all the data to LOGFONT
287 *logFont = pFI->lf;
288 }
289
290 wxFont wxCreateFontFromLogFont(const LOGFONT *logFont)
291 {
292 wxNativeFontInfo info;
293
294 info.lf = *logFont;
295
296 return wxFont(info);
297 }
298