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 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/string.h"
31 #include "wx/encinfo.h"
32 #include "wx/wxcrtvararg.h"
35 #include "wx/msw/private.h"
37 #include "wx/fontutil.h"
38 #include "wx/fontmap.h"
40 #include "wx/tokenzr.h"
42 // for MSVC5 and old w32api
43 #ifndef HANGUL_CHARSET
44 # define HANGUL_CHARSET 129
47 // ============================================================================
49 // ============================================================================
51 // ----------------------------------------------------------------------------
52 // wxNativeEncodingInfo
53 // ----------------------------------------------------------------------------
55 // convert to/from the string representation: format is
56 // encodingid;facename[;charset]
58 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
60 wxStringTokenizer
tokenizer(s
, wxT(";"));
62 wxString encid
= tokenizer
.GetNextToken();
64 // we support 2 formats: the old one (and still used if !wxUSE_FONTMAP)
65 // used the raw encoding values but the new one uses the encoding names
67 if ( encid
.ToLong(&enc
) )
69 // old format, intepret as encoding -- but after minimal checks
70 if ( enc
< 0 || enc
>= wxFONTENCODING_MAX
)
73 encoding
= (wxFontEncoding
)enc
;
75 else // not a number, interpret as an encoding name
78 encoding
= wxFontMapper::GetEncodingFromName(encid
);
79 if ( encoding
== wxFONTENCODING_MAX
)
80 #endif // wxUSE_FONTMAP
82 // failed to parse the name (or couldn't even try...)
87 facename
= tokenizer
.GetNextToken();
89 wxString tmp
= tokenizer
.GetNextToken();
92 // default charset: but don't use DEFAULT_CHARSET here because it might
93 // be different from the machine on which the file we had read this
94 // encoding desc from was created
95 charset
= ANSI_CHARSET
;
99 if ( wxSscanf(tmp
, wxT("%u"), &charset
) != 1 )
101 // should be a number!
109 wxString
wxNativeEncodingInfo::ToString() const
115 // use the encoding names as this is safer than using the numerical
116 // values which may change with time (because new encodings are
118 << wxFontMapper::GetEncodingName(encoding
)
119 #else // !wxUSE_FONTMAP
120 // we don't have any choice but to use the raw value
122 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
123 << wxT(';') << facename
;
125 // ANSI_CHARSET is assumed anyhow
126 if ( charset
!= ANSI_CHARSET
)
128 s
<< wxT(';') << charset
;
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
139 wxNativeEncodingInfo
*info
)
141 wxCHECK_MSG( info
, false, wxT("bad pointer in wxGetNativeFontEncoding") );
143 if ( encoding
== wxFONTENCODING_DEFAULT
)
145 encoding
= wxFont::GetDefaultEncoding();
148 extern WXDLLIMPEXP_BASE
long wxEncodingToCharset(wxFontEncoding encoding
);
149 info
->charset
= wxEncodingToCharset(encoding
);
150 if ( info
->charset
== -1 )
153 info
->encoding
= encoding
;
158 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
160 // try to create such font
162 wxZeroMemory(lf
); // all default values
164 lf
.lfCharSet
= (BYTE
)info
.charset
;
165 wxStrlcpy(lf
.lfFaceName
, info
.facename
.c_str(), WXSIZEOF(lf
.lfFaceName
));
167 HFONT hfont
= ::CreateFontIndirect(&lf
);
174 ::DeleteObject((HGDIOBJ
)hfont
);
179 // ----------------------------------------------------------------------------
180 // wxFontEncoding <-> CHARSET_XXX
181 // ----------------------------------------------------------------------------
183 wxFontEncoding
wxGetFontEncFromCharSet(int cs
)
185 wxFontEncoding fontEncoding
;
190 wxFAIL_MSG( wxT("unexpected Win32 charset") );
191 // fall through and assume the system charset
193 case DEFAULT_CHARSET
:
194 fontEncoding
= wxFONTENCODING_SYSTEM
;
198 fontEncoding
= wxFONTENCODING_CP1252
;
202 // what can we do here?
203 fontEncoding
= wxFONTENCODING_MAX
;
206 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
207 case EASTEUROPE_CHARSET
:
208 fontEncoding
= wxFONTENCODING_CP1250
;
212 fontEncoding
= wxFONTENCODING_CP1257
;
215 case RUSSIAN_CHARSET
:
216 fontEncoding
= wxFONTENCODING_CP1251
;
220 fontEncoding
= wxFONTENCODING_CP1256
;
224 fontEncoding
= wxFONTENCODING_CP1253
;
228 fontEncoding
= wxFONTENCODING_CP1255
;
231 case TURKISH_CHARSET
:
232 fontEncoding
= wxFONTENCODING_CP1254
;
236 fontEncoding
= wxFONTENCODING_CP874
;
239 case SHIFTJIS_CHARSET
:
240 fontEncoding
= wxFONTENCODING_CP932
;
244 fontEncoding
= wxFONTENCODING_CP936
;
248 fontEncoding
= wxFONTENCODING_CP949
;
251 case CHINESEBIG5_CHARSET
:
252 fontEncoding
= wxFONTENCODING_CP950
;
258 fontEncoding
= wxFONTENCODING_CP437
;
265 // ----------------------------------------------------------------------------
266 // wxFont <-> LOGFONT conversion
267 // ----------------------------------------------------------------------------
269 void wxFillLogFont(LOGFONT
*logFont
, const wxFont
*font
)
273 // maybe we already have LOGFONT for this font?
274 const wxNativeFontInfo
*pFI
= font
->GetNativeFontInfo();
277 // use wxNativeFontInfo methods to build a LOGFONT for this font
278 fi
.InitFromFont(*font
);
283 // transfer all the data to LOGFONT
287 wxFont
wxCreateFontFromLogFont(const LOGFONT
*logFont
)
289 wxNativeFontInfo info
;