1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/fontutil.cpp
3 // Purpose: font-related helper functions for wxMSW
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #include "wx/fontutil.h"
29 #include "wx/string.h"
32 #include "wx/wxcrtvararg.h"
33 #include "wx/msw/private.h"
36 #include "wx/encinfo.h"
37 #include "wx/fontmap.h"
38 #include "wx/tokenzr.h"
40 // for MSVC5 and old w32api
41 #ifndef HANGUL_CHARSET
42 # define HANGUL_CHARSET 129
45 // ============================================================================
47 // ============================================================================
49 // ----------------------------------------------------------------------------
50 // wxNativeEncodingInfo
51 // ----------------------------------------------------------------------------
53 // convert to/from the string representation: format is
54 // encodingid;facename[;charset]
56 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
58 wxStringTokenizer
tokenizer(s
, wxT(";"));
60 wxString encid
= tokenizer
.GetNextToken();
62 // we support 2 formats: the old one (and still used if !wxUSE_FONTMAP)
63 // used the raw encoding values but the new one uses the encoding names
65 if ( encid
.ToLong(&enc
) )
67 // old format, intepret as encoding -- but after minimal checks
68 if ( enc
< 0 || enc
>= wxFONTENCODING_MAX
)
71 encoding
= (wxFontEncoding
)enc
;
73 else // not a number, interpret as an encoding name
76 encoding
= wxFontMapper::GetEncodingFromName(encid
);
77 if ( encoding
== wxFONTENCODING_MAX
)
78 #endif // wxUSE_FONTMAP
80 // failed to parse the name (or couldn't even try...)
85 facename
= tokenizer
.GetNextToken();
87 wxString tmp
= tokenizer
.GetNextToken();
90 // default charset: but don't use DEFAULT_CHARSET here because it might
91 // be different from the machine on which the file we had read this
92 // encoding desc from was created
93 charset
= ANSI_CHARSET
;
97 if ( wxSscanf(tmp
, wxT("%u"), &charset
) != 1 )
99 // should be a number!
107 wxString
wxNativeEncodingInfo::ToString() const
113 // use the encoding names as this is safer than using the numerical
114 // values which may change with time (because new encodings are
116 << wxFontMapper::GetEncodingName(encoding
)
117 #else // !wxUSE_FONTMAP
118 // we don't have any choice but to use the raw value
120 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
121 << wxT(';') << facename
;
123 // ANSI_CHARSET is assumed anyhow
124 if ( charset
!= ANSI_CHARSET
)
126 s
<< wxT(';') << charset
;
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
137 wxNativeEncodingInfo
*info
)
139 wxCHECK_MSG( info
, false, wxT("bad pointer in wxGetNativeFontEncoding") );
141 if ( encoding
== wxFONTENCODING_DEFAULT
)
143 encoding
= wxFont::GetDefaultEncoding();
146 extern WXDLLIMPEXP_BASE
long wxEncodingToCharset(wxFontEncoding encoding
);
147 info
->charset
= wxEncodingToCharset(encoding
);
148 if ( info
->charset
== -1 )
151 info
->encoding
= encoding
;
156 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
158 // try to create such font
160 wxZeroMemory(lf
); // all default values
162 lf
.lfCharSet
= (BYTE
)info
.charset
;
163 wxStrlcpy(lf
.lfFaceName
, info
.facename
.c_str(), WXSIZEOF(lf
.lfFaceName
));
165 HFONT hfont
= ::CreateFontIndirect(&lf
);
172 ::DeleteObject((HGDIOBJ
)hfont
);
177 // ----------------------------------------------------------------------------
178 // wxFontEncoding <-> CHARSET_XXX
179 // ----------------------------------------------------------------------------
181 wxFontEncoding
wxGetFontEncFromCharSet(int cs
)
183 wxFontEncoding fontEncoding
;
188 wxFAIL_MSG( wxT("unexpected Win32 charset") );
189 // fall through and assume the system charset
191 case DEFAULT_CHARSET
:
192 fontEncoding
= wxFONTENCODING_SYSTEM
;
196 fontEncoding
= wxFONTENCODING_CP1252
;
200 // what can we do here?
201 fontEncoding
= wxFONTENCODING_MAX
;
204 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
205 case EASTEUROPE_CHARSET
:
206 fontEncoding
= wxFONTENCODING_CP1250
;
210 fontEncoding
= wxFONTENCODING_CP1257
;
213 case RUSSIAN_CHARSET
:
214 fontEncoding
= wxFONTENCODING_CP1251
;
218 fontEncoding
= wxFONTENCODING_CP1256
;
222 fontEncoding
= wxFONTENCODING_CP1253
;
226 fontEncoding
= wxFONTENCODING_CP1255
;
229 case TURKISH_CHARSET
:
230 fontEncoding
= wxFONTENCODING_CP1254
;
234 fontEncoding
= wxFONTENCODING_CP874
;
237 case SHIFTJIS_CHARSET
:
238 fontEncoding
= wxFONTENCODING_CP932
;
242 fontEncoding
= wxFONTENCODING_CP936
;
246 fontEncoding
= wxFONTENCODING_CP949
;
249 case CHINESEBIG5_CHARSET
:
250 fontEncoding
= wxFONTENCODING_CP950
;
253 case VIETNAMESE_CHARSET
:
254 fontEncoding
= wxFONTENCODING_CP1258
;
258 fontEncoding
= wxFONTENCODING_CP1361
;
264 fontEncoding
= wxFONTENCODING_CP437
;
271 // ----------------------------------------------------------------------------
272 // wxFont <-> LOGFONT conversion
273 // ----------------------------------------------------------------------------
275 void wxFillLogFont(LOGFONT
*logFont
, const wxFont
*font
)
279 // maybe we already have LOGFONT for this font?
280 const wxNativeFontInfo
*pFI
= font
->GetNativeFontInfo();
283 // use wxNativeFontInfo methods to build a LOGFONT for this font
284 fi
.InitFromFont(*font
);
289 // transfer all the data to LOGFONT
293 wxFont
wxCreateFontFromLogFont(const LOGFONT
*logFont
)
295 wxNativeFontInfo info
;