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"
34 #include "wx/msw/private.h"
36 #include "wx/fontutil.h"
37 #include "wx/fontmap.h"
39 #include "wx/tokenzr.h"
41 // for MSVC5 and old w32api
42 #ifndef HANGUL_CHARSET
43 # define HANGUL_CHARSET 129
46 // ============================================================================
48 // ============================================================================
50 // ----------------------------------------------------------------------------
51 // wxNativeEncodingInfo
52 // ----------------------------------------------------------------------------
54 // convert to/from the string representation: format is
55 // encodingid;facename[;charset]
57 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
59 wxStringTokenizer
tokenizer(s
, _T(";"));
61 wxString encid
= tokenizer
.GetNextToken();
63 // we support 2 formats: the old one (and still used if !wxUSE_FONTMAP)
64 // used the raw encoding values but the new one uses the encoding names
66 if ( encid
.ToLong(&enc
) )
68 // old format, intepret as encoding -- but after minimal checks
69 if ( enc
< 0 || enc
>= wxFONTENCODING_MAX
)
72 encoding
= (wxFontEncoding
)enc
;
74 else // not a number, interpret as an encoding name
77 encoding
= wxFontMapper::GetEncodingFromName(encid
);
78 if ( encoding
== wxFONTENCODING_MAX
)
79 #endif // wxUSE_FONTMAP
81 // failed to parse the name (or couldn't even try...)
86 facename
= tokenizer
.GetNextToken();
88 wxString tmp
= tokenizer
.GetNextToken();
91 // default charset: but don't use DEFAULT_CHARSET here because it might
92 // be different from the machine on which the file we had read this
93 // encoding desc from was created
94 charset
= ANSI_CHARSET
;
98 if ( wxSscanf(tmp
, _T("%u"), &charset
) != 1 )
100 // should be a number!
108 wxString
wxNativeEncodingInfo::ToString() const
114 // use the encoding names as this is safer than using the numerical
115 // values which may change with time (because new encodings are
117 << wxFontMapper::GetEncodingName(encoding
)
118 #else // !wxUSE_FONTMAP
119 // we don't have any choice but to use the raw value
121 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
122 << _T(';') << facename
;
124 // ANSI_CHARSET is assumed anyhow
125 if ( charset
!= ANSI_CHARSET
)
127 s
<< _T(';') << charset
;
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
138 wxNativeEncodingInfo
*info
)
140 wxCHECK_MSG( info
, false, _T("bad pointer in wxGetNativeFontEncoding") );
142 if ( encoding
== wxFONTENCODING_DEFAULT
)
144 encoding
= wxFont::GetDefaultEncoding();
147 extern WXDLLIMPEXP_BASE
long wxEncodingToCharset(wxFontEncoding encoding
);
148 info
->charset
= wxEncodingToCharset(encoding
);
149 if ( info
->charset
== -1 )
152 info
->encoding
= encoding
;
157 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
159 // try to create such font
161 wxZeroMemory(lf
); // all default values
163 lf
.lfCharSet
= (BYTE
)info
.charset
;
164 wxStrncpy(lf
.lfFaceName
, info
.facename
, WXSIZEOF(lf
.lfFaceName
));
166 HFONT hfont
= ::CreateFontIndirect(&lf
);
173 ::DeleteObject((HGDIOBJ
)hfont
);
178 // ----------------------------------------------------------------------------
179 // wxFontEncoding <-> CHARSET_XXX
180 // ----------------------------------------------------------------------------
182 wxFontEncoding
wxGetFontEncFromCharSet(int cs
)
184 wxFontEncoding fontEncoding
;
189 wxFAIL_MSG( _T("unexpected Win32 charset") );
190 // fall through and assume the system charset
192 case DEFAULT_CHARSET
:
193 fontEncoding
= wxFONTENCODING_SYSTEM
;
197 fontEncoding
= wxFONTENCODING_CP1252
;
201 // what can we do here?
202 fontEncoding
= wxFONTENCODING_MAX
;
205 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
206 case EASTEUROPE_CHARSET
:
207 fontEncoding
= wxFONTENCODING_CP1250
;
211 fontEncoding
= wxFONTENCODING_CP1257
;
214 case RUSSIAN_CHARSET
:
215 fontEncoding
= wxFONTENCODING_CP1251
;
219 fontEncoding
= wxFONTENCODING_CP1256
;
223 fontEncoding
= wxFONTENCODING_CP1253
;
227 fontEncoding
= wxFONTENCODING_CP1255
;
230 case TURKISH_CHARSET
:
231 fontEncoding
= wxFONTENCODING_CP1254
;
235 fontEncoding
= wxFONTENCODING_CP874
;
238 case SHIFTJIS_CHARSET
:
239 fontEncoding
= wxFONTENCODING_CP932
;
243 fontEncoding
= wxFONTENCODING_CP936
;
247 fontEncoding
= wxFONTENCODING_CP949
;
250 case CHINESEBIG5_CHARSET
:
251 fontEncoding
= wxFONTENCODING_CP950
;
257 fontEncoding
= wxFONTENCODING_CP437
;
264 // ----------------------------------------------------------------------------
265 // wxFont <-> LOGFONT conversion
266 // ----------------------------------------------------------------------------
268 void wxFillLogFont(LOGFONT
*logFont
, const wxFont
*font
)
272 // maybe we already have LOGFONT for this font?
273 const wxNativeFontInfo
*pFI
= font
->GetNativeFontInfo();
276 // use wxNativeFontInfo methods to build a LOGFONT for this font
277 fi
.InitFromFont(*font
);
282 // transfer all the data to LOGFONT
286 wxFont
wxCreateFontFromLogFont(const LOGFONT
*logFont
)
288 wxNativeFontInfo info
;