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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "fontutil.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/string.h"
35 #include "wx/encinfo.h"
38 #include "wx/msw/private.h"
40 #include "wx/fontutil.h"
41 #include "wx/fontmap.h"
43 #include "wx/tokenzr.h"
45 // for MSVC5 and old w32api
46 #ifndef HANGUL_CHARSET
47 # define HANGUL_CHARSET 129
50 // ============================================================================
52 // ============================================================================
54 // ----------------------------------------------------------------------------
55 // wxNativeEncodingInfo
56 // ----------------------------------------------------------------------------
58 // convert to/from the string representation: format is
59 // encodingid;facename[;charset]
61 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
63 wxStringTokenizer
tokenizer(s
, _T(";"));
65 wxString encid
= tokenizer
.GetNextToken();
67 // we support 2 formats: the old one (and still used if !wxUSE_FONTMAP)
68 // used the raw encoding values but the new one uses the encoding names
70 if ( encid
.ToLong(&enc
) )
72 // old format, intepret as encoding -- but after minimal checks
73 if ( enc
< 0 || enc
>= wxFONTENCODING_MAX
)
76 encoding
= (wxFontEncoding
)enc
;
78 else // not a number, interpret as an encoding name
81 encoding
= wxFontMapper::GetEncodingFromName(encid
);
82 if ( encoding
== wxFONTENCODING_MAX
)
83 #endif // wxUSE_FONTMAP
85 // failed to parse the name (or couldn't even try...)
90 facename
= tokenizer
.GetNextToken();
92 wxString tmp
= tokenizer
.GetNextToken();
95 // default charset: but don't use DEFAULT_CHARSET here because it might
96 // be different from the machine on which the file we had read this
97 // encoding desc from was created
98 charset
= ANSI_CHARSET
;
102 if ( wxSscanf(tmp
, _T("%u"), &charset
) != 1 )
104 // should be a number!
112 wxString
wxNativeEncodingInfo::ToString() const
118 // use the encoding names as this is safer than using the numerical
119 // values which may change with time (because new encodings are
121 << wxFontMapper::GetEncodingName(encoding
)
122 #else // !wxUSE_FONTMAP
123 // we don't have any choice but to use the raw value
125 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
126 << _T(';') << facename
;
128 // ANSI_CHARSET is assumed anyhow
129 if ( charset
!= ANSI_CHARSET
)
131 s
<< _T(';') << charset
;
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
142 wxNativeEncodingInfo
*info
)
144 wxCHECK_MSG( info
, false, _T("bad pointer in wxGetNativeFontEncoding") );
146 if ( encoding
== wxFONTENCODING_DEFAULT
)
148 encoding
= wxFont::GetDefaultEncoding();
151 extern WXDLLIMPEXP_BASE
long wxEncodingToCharset(wxFontEncoding encoding
);
152 info
->charset
= wxEncodingToCharset(encoding
);
153 if ( info
->charset
== -1 )
156 info
->encoding
= encoding
;
161 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
163 // try to create such font
165 wxZeroMemory(lf
); // all default values
167 lf
.lfCharSet
= info
.charset
;
168 wxStrncpy(lf
.lfFaceName
, info
.facename
, WXSIZEOF(lf
.lfFaceName
));
170 HFONT hfont
= ::CreateFontIndirect(&lf
);
177 ::DeleteObject((HGDIOBJ
)hfont
);
182 // ----------------------------------------------------------------------------
183 // wxFontEncoding <-> CHARSET_XXX
184 // ----------------------------------------------------------------------------
186 wxFontEncoding
wxGetFontEncFromCharSet(int cs
)
188 wxFontEncoding fontEncoding
;
193 wxFAIL_MSG( _T("unexpected Win32 charset") );
194 // fall through and assume the system charset
196 case DEFAULT_CHARSET
:
197 fontEncoding
= wxFONTENCODING_SYSTEM
;
201 fontEncoding
= wxFONTENCODING_CP1252
;
205 // what can we do here?
206 fontEncoding
= wxFONTENCODING_MAX
;
209 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
210 case EASTEUROPE_CHARSET
:
211 fontEncoding
= wxFONTENCODING_CP1250
;
215 fontEncoding
= wxFONTENCODING_CP1257
;
218 case RUSSIAN_CHARSET
:
219 fontEncoding
= wxFONTENCODING_CP1251
;
223 fontEncoding
= wxFONTENCODING_CP1256
;
227 fontEncoding
= wxFONTENCODING_CP1253
;
231 fontEncoding
= wxFONTENCODING_CP1255
;
234 case TURKISH_CHARSET
:
235 fontEncoding
= wxFONTENCODING_CP1254
;
239 fontEncoding
= wxFONTENCODING_CP437
;
242 case SHIFTJIS_CHARSET
:
243 fontEncoding
= wxFONTENCODING_CP932
;
247 fontEncoding
= wxFONTENCODING_CP936
;
251 fontEncoding
= wxFONTENCODING_CP949
;
254 case CHINESEBIG5_CHARSET
:
255 fontEncoding
= wxFONTENCODING_CP950
;
261 fontEncoding
= wxFONTENCODING_CP437
;
268 // ----------------------------------------------------------------------------
269 // wxFont <-> LOGFONT conversion
270 // ----------------------------------------------------------------------------
272 void wxFillLogFont(LOGFONT
*logFont
, const wxFont
*font
)
276 // maybe we already have LOGFONT for this font?
277 const wxNativeFontInfo
*pFI
= font
->GetNativeFontInfo();
280 // use wxNativeFontInfo methods to build a LOGFONT for this font
281 fi
.InitFromFont(*font
);
286 // transfer all the data to LOGFONT
290 wxFont
wxCreateFontFromLogFont(const LOGFONT
*logFont
)
292 wxNativeFontInfo info
;