1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/fontutil.cpp
3 // Purpose: font-related helper functions for wxMSW
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "fontutil.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/string.h"
37 #include "wx/msw/private.h"
39 #include "wx/fontutil.h"
40 #include "wx/fontmap.h"
42 #include "wx/tokenzr.h"
44 // for MSVC5 and old w32api
45 #ifndef HANGUL_CHARSET
46 # define HANGUL_CHARSET 129
49 // ============================================================================
51 // ============================================================================
53 // ----------------------------------------------------------------------------
54 // wxNativeEncodingInfo
55 // ----------------------------------------------------------------------------
57 // convert to/from the string representation: format is
58 // encodingid;facename[;charset]
60 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
62 wxStringTokenizer
tokenizer(s
, _T(";"));
64 wxString encid
= tokenizer
.GetNextToken();
66 if ( !encid
.ToLong(&enc
) )
68 encoding
= (wxFontEncoding
)enc
;
70 facename
= tokenizer
.GetNextToken();
72 wxString tmp
= tokenizer
.GetNextToken();
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
;
82 if ( wxSscanf(tmp
, _T("%u"), &charset
) != 1 )
84 // should be a number!
92 wxString
wxNativeEncodingInfo::ToString() const
96 s
<< (long)encoding
<< _T(';') << facename
;
98 // ANSI_CHARSET is assumed anyhow
99 if ( charset
!= ANSI_CHARSET
)
101 s
<< _T(';') << charset
;
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
112 wxNativeEncodingInfo
*info
)
114 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
116 if ( encoding
== wxFONTENCODING_DEFAULT
)
118 encoding
= wxFont::GetDefaultEncoding();
123 // although this function is supposed to return an exact match, do do
124 // some mappings here for the most common case of "standard" encoding
125 case wxFONTENCODING_SYSTEM
:
126 info
->charset
= DEFAULT_CHARSET
;
129 case wxFONTENCODING_ISO8859_1
:
130 case wxFONTENCODING_ISO8859_15
:
131 case wxFONTENCODING_CP1252
:
132 info
->charset
= ANSI_CHARSET
;
135 #if !defined(__WIN16__) && !defined(__WXMICROWIN__)
137 // The following four fonts are multi-byte charsets
138 case wxFONTENCODING_CP932
:
139 info
->charset
= SHIFTJIS_CHARSET
;
142 case wxFONTENCODING_CP936
:
143 info
->charset
= GB2312_CHARSET
;
146 case wxFONTENCODING_CP949
:
147 info
->charset
= HANGUL_CHARSET
;
150 case wxFONTENCODING_CP950
:
151 info
->charset
= CHINESEBIG5_CHARSET
;
154 // The rest are single byte encodings
155 case wxFONTENCODING_CP1250
:
156 info
->charset
= EASTEUROPE_CHARSET
;
159 case wxFONTENCODING_CP1251
:
160 info
->charset
= RUSSIAN_CHARSET
;
163 case wxFONTENCODING_CP1253
:
164 info
->charset
= GREEK_CHARSET
;
167 case wxFONTENCODING_CP1254
:
168 info
->charset
= TURKISH_CHARSET
;
171 case wxFONTENCODING_CP1255
:
172 info
->charset
= HEBREW_CHARSET
;
175 case wxFONTENCODING_CP1256
:
176 info
->charset
= ARABIC_CHARSET
;
179 case wxFONTENCODING_CP1257
:
180 info
->charset
= BALTIC_CHARSET
;
183 case wxFONTENCODING_CP874
:
184 info
->charset
= THAI_CHARSET
;
190 case wxFONTENCODING_CP437
:
191 info
->charset
= OEM_CHARSET
;
195 // no way to translate this encoding into a Windows charset
199 info
->encoding
= encoding
;
204 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
206 // try to create such font
208 wxZeroMemory(lf
); // all default values
210 lf
.lfCharSet
= info
.charset
;
211 wxStrncpy(lf
.lfFaceName
, info
.facename
, WXSIZEOF(lf
.lfFaceName
));
213 HFONT hfont
= ::CreateFontIndirect(&lf
);
220 ::DeleteObject((HGDIOBJ
)hfont
);
225 // ----------------------------------------------------------------------------
226 // wxFontEncoding <-> CHARSET_XXX
227 // ----------------------------------------------------------------------------
229 wxFontEncoding
wxGetFontEncFromCharSet(int cs
)
231 wxFontEncoding fontEncoding
;
236 // assume the system charset
237 fontEncoding
= wxFONTENCODING_SYSTEM
;
241 fontEncoding
= wxFONTENCODING_CP1252
;
244 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
245 case EASTEUROPE_CHARSET
:
246 fontEncoding
= wxFONTENCODING_CP1250
;
250 fontEncoding
= wxFONTENCODING_CP1257
;
253 case RUSSIAN_CHARSET
:
254 fontEncoding
= wxFONTENCODING_CP1251
;
258 fontEncoding
= wxFONTENCODING_CP1256
;
262 fontEncoding
= wxFONTENCODING_CP1253
;
266 fontEncoding
= wxFONTENCODING_CP1255
;
269 case TURKISH_CHARSET
:
270 fontEncoding
= wxFONTENCODING_CP1254
;
274 fontEncoding
= wxFONTENCODING_CP437
;
277 case SHIFTJIS_CHARSET
:
278 fontEncoding
= wxFONTENCODING_CP932
;
282 fontEncoding
= wxFONTENCODING_CP936
;
286 fontEncoding
= wxFONTENCODING_CP949
;
289 case CHINESEBIG5_CHARSET
:
290 fontEncoding
= wxFONTENCODING_CP950
;
296 fontEncoding
= wxFONTENCODING_CP437
;
303 // ----------------------------------------------------------------------------
304 // wxFont <-> LOGFONT conversion
305 // ----------------------------------------------------------------------------
307 void wxFillLogFont(LOGFONT
*logFont
, const wxFont
*font
)
309 // maybe we already have LOGFONT for this font?
310 wxNativeFontInfo
*fontinfo
= font
->GetNativeFontInfo();
313 // use wxNativeFontInfo methods to build a LOGFONT for this font
314 fontinfo
= new wxNativeFontInfo
;
316 // translate all font parameters
317 fontinfo
->SetStyle((wxFontStyle
)font
->GetStyle());
318 fontinfo
->SetWeight((wxFontWeight
)font
->GetWeight());
319 fontinfo
->SetUnderlined(font
->GetUnderlined());
320 fontinfo
->SetPointSize(font
->GetPointSize());
322 // set the family/facename
323 fontinfo
->SetFamily((wxFontFamily
)font
->GetFamily());
324 wxString facename
= font
->GetFaceName();
325 if ( !facename
.empty() )
327 fontinfo
->SetFaceName(facename
);
330 // deal with encoding now (it may override the font family and facename
331 // so do it after setting them)
332 fontinfo
->SetEncoding(font
->GetEncoding());
335 // transfer all the data to LOGFONT
336 *logFont
= fontinfo
->lf
;
341 wxFont
wxCreateFontFromLogFont(const LOGFONT
*logFont
)
343 wxNativeFontInfo info
;