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 license
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 (don't use DEFAULT_CHARSET though because of subtle
76 // Windows 9x/NT differences in handling it)
77 charset
= ANSI_CHARSET
;
81 if ( wxSscanf(tmp
, _T("%u"), &charset
) != 1 )
83 // should be a number!
91 wxString
wxNativeEncodingInfo::ToString() const
95 s
<< (long)encoding
<< _T(';') << facename
;
96 if ( charset
!= ANSI_CHARSET
)
98 s
<< _T(';') << charset
;
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
109 wxNativeEncodingInfo
*info
)
111 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
113 if ( encoding
== wxFONTENCODING_DEFAULT
)
115 encoding
= wxFont::GetDefaultEncoding();
120 // although this function is supposed to return an exact match, do do
121 // some mappings here for the most common case of "standard" encoding
122 case wxFONTENCODING_SYSTEM
:
123 case wxFONTENCODING_ISO8859_1
:
124 case wxFONTENCODING_ISO8859_15
:
125 case wxFONTENCODING_CP1252
:
126 info
->charset
= ANSI_CHARSET
;
129 #if !defined(__WIN16__) && !defined(__WXMICROWIN__)
131 // The following four fonts are multi-byte charsets
132 case wxFONTENCODING_CP932
:
133 info
->charset
= SHIFTJIS_CHARSET
;
136 case wxFONTENCODING_CP936
:
137 info
->charset
= GB2312_CHARSET
;
140 case wxFONTENCODING_CP949
:
141 info
->charset
= HANGUL_CHARSET
;
144 case wxFONTENCODING_CP950
:
145 info
->charset
= CHINESEBIG5_CHARSET
;
148 // The rest are single byte encodings
149 case wxFONTENCODING_CP1250
:
150 info
->charset
= EASTEUROPE_CHARSET
;
153 case wxFONTENCODING_CP1251
:
154 info
->charset
= RUSSIAN_CHARSET
;
157 case wxFONTENCODING_CP1253
:
158 info
->charset
= GREEK_CHARSET
;
161 case wxFONTENCODING_CP1254
:
162 info
->charset
= TURKISH_CHARSET
;
165 case wxFONTENCODING_CP1255
:
166 info
->charset
= HEBREW_CHARSET
;
169 case wxFONTENCODING_CP1256
:
170 info
->charset
= ARABIC_CHARSET
;
173 case wxFONTENCODING_CP1257
:
174 info
->charset
= BALTIC_CHARSET
;
177 case wxFONTENCODING_CP874
:
178 info
->charset
= THAI_CHARSET
;
184 case wxFONTENCODING_CP437
:
185 info
->charset
= OEM_CHARSET
;
189 // no way to translate this encoding into a Windows charset
193 info
->encoding
= encoding
;
198 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
200 // try to create such font
202 wxZeroMemory(lf
); // all default values
204 lf
.lfCharSet
= info
.charset
;
205 wxStrncpy(lf
.lfFaceName
, info
.facename
, WXSIZEOF(lf
.lfFaceName
));
207 HFONT hfont
= ::CreateFontIndirect(&lf
);
214 ::DeleteObject((HGDIOBJ
)hfont
);
219 // ----------------------------------------------------------------------------
220 // wxFontEncoding <-> CHARSET_XXX
221 // ----------------------------------------------------------------------------
223 wxFontEncoding
wxGetFontEncFromCharSet(int cs
)
225 wxFontEncoding fontEncoding
;
230 // JACS: Silently using ANSI_CHARSET
231 // apparently works for Chinese Windows. Assume it works
232 // for all/most other languages.
233 //wxFAIL_MSG(wxT("unsupported charset"));
237 fontEncoding
= wxFONTENCODING_CP1252
;
240 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
241 case EASTEUROPE_CHARSET
:
242 fontEncoding
= wxFONTENCODING_CP1250
;
246 fontEncoding
= wxFONTENCODING_CP1257
;
249 case RUSSIAN_CHARSET
:
250 fontEncoding
= wxFONTENCODING_CP1251
;
254 fontEncoding
= wxFONTENCODING_CP1256
;
258 fontEncoding
= wxFONTENCODING_CP1253
;
262 fontEncoding
= wxFONTENCODING_CP1255
;
265 case TURKISH_CHARSET
:
266 fontEncoding
= wxFONTENCODING_CP1254
;
270 fontEncoding
= wxFONTENCODING_CP437
;
273 case SHIFTJIS_CHARSET
:
274 fontEncoding
= wxFONTENCODING_CP932
;
278 fontEncoding
= wxFONTENCODING_CP936
;
282 fontEncoding
= wxFONTENCODING_CP949
;
285 case CHINESEBIG5_CHARSET
:
286 fontEncoding
= wxFONTENCODING_CP950
;
292 fontEncoding
= wxFONTENCODING_CP437
;
299 // ----------------------------------------------------------------------------
300 // wxFont <-> LOGFONT conversion
301 // ----------------------------------------------------------------------------
303 void wxFillLogFont(LOGFONT
*logFont
, const wxFont
*font
)
305 // maybe we already have LOGFONT for this font?
306 wxNativeFontInfo
*fontinfo
= font
->GetNativeFontInfo();
309 // use wxNativeFontInfo methods to build a LOGFONT for this font
310 fontinfo
= new wxNativeFontInfo
;
312 // translate all font parameters
313 fontinfo
->SetStyle((wxFontStyle
)font
->GetStyle());
314 fontinfo
->SetWeight((wxFontWeight
)font
->GetWeight());
315 fontinfo
->SetUnderlined(font
->GetUnderlined());
316 fontinfo
->SetPointSize(font
->GetPointSize());
318 // set the family/facename
319 fontinfo
->SetFamily((wxFontFamily
)font
->GetFamily());
320 wxString facename
= font
->GetFaceName();
321 if ( !facename
.empty() )
323 fontinfo
->SetFaceName(facename
);
326 // deal with encoding now (it may override the font family and facename
327 // so do it after setting them)
328 fontinfo
->SetEncoding(font
->GetEncoding());
331 // transfer all the data to LOGFONT
332 *logFont
= fontinfo
->lf
;
337 wxFont
wxCreateFontFromLogFont(const LOGFONT
*logFont
)
339 wxNativeFontInfo info
;