]> git.saurik.com Git - wxWidgets.git/blob - src/msw/fontutil.cpp
wxStrdup(NULL) doesn't work
[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 #endif //WX_PRECOMP
36
37 #include "wx/msw/private.h"
38
39 #include "wx/fontutil.h"
40 #include "wx/fontmap.h"
41
42 #include "wx/tokenzr.h"
43
44 // for MSVC5 and old w32api
45 #ifndef HANGUL_CHARSET
46 # define HANGUL_CHARSET 129
47 #endif
48
49 // ============================================================================
50 // implementation
51 // ============================================================================
52
53 // ----------------------------------------------------------------------------
54 // wxNativeEncodingInfo
55 // ----------------------------------------------------------------------------
56
57 // convert to/from the string representation: format is
58 // encodingid;facename[;charset]
59
60 bool wxNativeEncodingInfo::FromString(const wxString& s)
61 {
62 wxStringTokenizer tokenizer(s, _T(";"));
63
64 wxString encid = tokenizer.GetNextToken();
65 long enc;
66 if ( !encid.ToLong(&enc) )
67 return FALSE;
68 encoding = (wxFontEncoding)enc;
69
70 facename = tokenizer.GetNextToken();
71
72 wxString tmp = tokenizer.GetNextToken();
73 if ( tmp.empty() )
74 {
75 // default charset: but don't use DEFAULT_CHARSET here because it might
76 // be different from the machine on which the file we had read this
77 // encoding desc from was created
78 charset = ANSI_CHARSET;
79 }
80 else
81 {
82 if ( wxSscanf(tmp, _T("%u"), &charset) != 1 )
83 {
84 // should be a number!
85 return FALSE;
86 }
87 }
88
89 return TRUE;
90 }
91
92 wxString wxNativeEncodingInfo::ToString() const
93 {
94 wxString s;
95
96 s << (long)encoding << _T(';') << facename;
97
98 // ANSI_CHARSET is assumed anyhow
99 if ( charset != ANSI_CHARSET )
100 {
101 s << _T(';') << charset;
102 }
103
104 return s;
105 }
106
107 // ----------------------------------------------------------------------------
108 // helper functions
109 // ----------------------------------------------------------------------------
110
111 bool wxGetNativeFontEncoding(wxFontEncoding encoding,
112 wxNativeEncodingInfo *info)
113 {
114 wxCHECK_MSG( info, FALSE, _T("bad pointer in wxGetNativeFontEncoding") );
115
116 if ( encoding == wxFONTENCODING_DEFAULT )
117 {
118 encoding = wxFont::GetDefaultEncoding();
119 }
120
121 extern WXDLLIMPEXP_BASE long wxEncodingToCharset(wxFontEncoding encoding);
122 info->charset = wxEncodingToCharset(encoding);
123 if ( info->charset == -1 )
124 return FALSE;
125
126 info->encoding = encoding;
127
128 return TRUE;
129 }
130
131 bool wxTestFontEncoding(const wxNativeEncodingInfo& info)
132 {
133 // try to create such font
134 LOGFONT lf;
135 wxZeroMemory(lf); // all default values
136
137 lf.lfCharSet = info.charset;
138 wxStrncpy(lf.lfFaceName, info.facename, WXSIZEOF(lf.lfFaceName));
139
140 HFONT hfont = ::CreateFontIndirect(&lf);
141 if ( !hfont )
142 {
143 // no such font
144 return FALSE;
145 }
146
147 ::DeleteObject((HGDIOBJ)hfont);
148
149 return TRUE;
150 }
151
152 // ----------------------------------------------------------------------------
153 // wxFontEncoding <-> CHARSET_XXX
154 // ----------------------------------------------------------------------------
155
156 wxFontEncoding wxGetFontEncFromCharSet(int cs)
157 {
158 wxFontEncoding fontEncoding;
159
160 switch ( cs )
161 {
162 default:
163 // assume the system charset
164 fontEncoding = wxFONTENCODING_SYSTEM;
165 break;
166
167 case ANSI_CHARSET:
168 fontEncoding = wxFONTENCODING_CP1252;
169 break;
170
171 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
172 case EASTEUROPE_CHARSET:
173 fontEncoding = wxFONTENCODING_CP1250;
174 break;
175
176 case BALTIC_CHARSET:
177 fontEncoding = wxFONTENCODING_CP1257;
178 break;
179
180 case RUSSIAN_CHARSET:
181 fontEncoding = wxFONTENCODING_CP1251;
182 break;
183
184 case ARABIC_CHARSET:
185 fontEncoding = wxFONTENCODING_CP1256;
186 break;
187
188 case GREEK_CHARSET:
189 fontEncoding = wxFONTENCODING_CP1253;
190 break;
191
192 case HEBREW_CHARSET:
193 fontEncoding = wxFONTENCODING_CP1255;
194 break;
195
196 case TURKISH_CHARSET:
197 fontEncoding = wxFONTENCODING_CP1254;
198 break;
199
200 case THAI_CHARSET:
201 fontEncoding = wxFONTENCODING_CP437;
202 break;
203
204 case SHIFTJIS_CHARSET:
205 fontEncoding = wxFONTENCODING_CP932;
206 break;
207
208 case GB2312_CHARSET:
209 fontEncoding = wxFONTENCODING_CP936;
210 break;
211
212 case HANGUL_CHARSET:
213 fontEncoding = wxFONTENCODING_CP949;
214 break;
215
216 case CHINESEBIG5_CHARSET:
217 fontEncoding = wxFONTENCODING_CP950;
218 break;
219
220 #endif // Win32
221
222 case OEM_CHARSET:
223 fontEncoding = wxFONTENCODING_CP437;
224 break;
225 }
226
227 return fontEncoding;
228 }
229
230 // ----------------------------------------------------------------------------
231 // wxFont <-> LOGFONT conversion
232 // ----------------------------------------------------------------------------
233
234 void wxFillLogFont(LOGFONT *logFont, const wxFont *font)
235 {
236 // maybe we already have LOGFONT for this font?
237 wxNativeFontInfo *fontinfo = font->GetNativeFontInfo();
238 if ( !fontinfo )
239 {
240 // use wxNativeFontInfo methods to build a LOGFONT for this font
241 fontinfo = new wxNativeFontInfo;
242
243 // translate all font parameters
244 fontinfo->SetStyle((wxFontStyle)font->GetStyle());
245 fontinfo->SetWeight((wxFontWeight)font->GetWeight());
246 fontinfo->SetUnderlined(font->GetUnderlined());
247 fontinfo->SetPointSize(font->GetPointSize());
248
249 // set the family/facename
250 fontinfo->SetFamily((wxFontFamily)font->GetFamily());
251 wxString facename = font->GetFaceName();
252 if ( !facename.empty() )
253 {
254 fontinfo->SetFaceName(facename);
255 }
256
257 // deal with encoding now (it may override the font family and facename
258 // so do it after setting them)
259 fontinfo->SetEncoding(font->GetEncoding());
260 }
261
262 // transfer all the data to LOGFONT
263 *logFont = fontinfo->lf;
264
265 delete fontinfo;
266 }
267
268 wxFont wxCreateFontFromLogFont(const LOGFONT *logFont)
269 {
270 wxNativeFontInfo info;
271
272 info.lf = *logFont;
273
274 return wxFont(info);
275 }
276