]> git.saurik.com Git - wxWidgets.git/blame - src/msw/fontutil.cpp
don't recompute nonbreakable space character every time a text fragment is added...
[wxWidgets.git] / src / msw / fontutil.cpp
CommitLineData
11c7d5b6
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: msw/fontutil.cpp
3// Purpose: font-related helper functions for wxMSW
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 05.11.99
7// RCS-ID: $Id$
8// Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 9// Licence: wxWindows licence
11c7d5b6
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
11c7d5b6
VZ
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27#ifndef WX_PRECOMP
28 #include "wx/string.h"
29 #include "wx/log.h"
30 #include "wx/intl.h"
e4ffab29 31 #include "wx/encinfo.h"
193d0c93 32 #include "wx/wxcrtvararg.h"
11c7d5b6
VZ
33#endif //WX_PRECOMP
34
35#include "wx/msw/private.h"
36
37#include "wx/fontutil.h"
38#include "wx/fontmap.h"
39
40#include "wx/tokenzr.h"
41
e757496e
MB
42// for MSVC5 and old w32api
43#ifndef HANGUL_CHARSET
44# define HANGUL_CHARSET 129
45#endif
46
11c7d5b6
VZ
47// ============================================================================
48// implementation
49// ============================================================================
50
51// ----------------------------------------------------------------------------
52// wxNativeEncodingInfo
53// ----------------------------------------------------------------------------
54
55// convert to/from the string representation: format is
1e1d0be1 56// encodingid;facename[;charset]
11c7d5b6
VZ
57
58bool wxNativeEncodingInfo::FromString(const wxString& s)
59{
60 wxStringTokenizer tokenizer(s, _T(";"));
61
1e1d0be1 62 wxString encid = tokenizer.GetNextToken();
24056b9e
VZ
63
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
1e1d0be1 66 long enc;
24056b9e
VZ
67 if ( encid.ToLong(&enc) )
68 {
69 // old format, intepret as encoding -- but after minimal checks
70 if ( enc < 0 || enc >= wxFONTENCODING_MAX )
71 return false;
72
73 encoding = (wxFontEncoding)enc;
74 }
75 else // not a number, interpret as an encoding name
76 {
77#if wxUSE_FONTMAP
78 encoding = wxFontMapper::GetEncodingFromName(encid);
79 if ( encoding == wxFONTENCODING_MAX )
80#endif // wxUSE_FONTMAP
81 {
82 // failed to parse the name (or couldn't even try...)
83 return false;
84 }
85 }
1e1d0be1 86
11c7d5b6 87 facename = tokenizer.GetNextToken();
11c7d5b6
VZ
88
89 wxString tmp = tokenizer.GetNextToken();
4ec6efd6 90 if ( tmp.empty() )
11c7d5b6 91 {
4ec6efd6
VZ
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
11c7d5b6
VZ
95 charset = ANSI_CHARSET;
96 }
97 else
98 {
99 if ( wxSscanf(tmp, _T("%u"), &charset) != 1 )
100 {
101 // should be a number!
cbe874bd 102 return false;
11c7d5b6
VZ
103 }
104 }
105
cbe874bd 106 return true;
11c7d5b6
VZ
107}
108
109wxString wxNativeEncodingInfo::ToString() const
110{
1e1d0be1 111 wxString s;
f6bcfd97 112
24056b9e
VZ
113 s
114#if wxUSE_FONTMAP
115 // use the encoding names as this is safer than using the numerical
116 // values which may change with time (because new encodings are
117 // inserted...)
118 << wxFontMapper::GetEncodingName(encoding)
119#else // !wxUSE_FONTMAP
120 // we don't have any choice but to use the raw value
121 << (long)encoding
122#endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
123 << _T(';') << facename;
4ec6efd6
VZ
124
125 // ANSI_CHARSET is assumed anyhow
11c7d5b6
VZ
126 if ( charset != ANSI_CHARSET )
127 {
1e1d0be1 128 s << _T(';') << charset;
11c7d5b6
VZ
129 }
130
131 return s;
132}
133
134// ----------------------------------------------------------------------------
135// helper functions
136// ----------------------------------------------------------------------------
137
138bool wxGetNativeFontEncoding(wxFontEncoding encoding,
139 wxNativeEncodingInfo *info)
140{
2bee3033 141 wxCHECK_MSG( info, false, _T("bad pointer in wxGetNativeFontEncoding") );
11c7d5b6
VZ
142
143 if ( encoding == wxFONTENCODING_DEFAULT )
144 {
145 encoding = wxFont::GetDefaultEncoding();
146 }
147
bddd7a8d 148 extern WXDLLIMPEXP_BASE long wxEncodingToCharset(wxFontEncoding encoding);
e2478fde
VZ
149 info->charset = wxEncodingToCharset(encoding);
150 if ( info->charset == -1 )
cbe874bd 151 return false;
11c7d5b6 152
6b0eebc5 153 info->encoding = encoding;
f6bcfd97 154
cbe874bd 155 return true;
11c7d5b6
VZ
156}
157
158bool wxTestFontEncoding(const wxNativeEncodingInfo& info)
159{
160 // try to create such font
161 LOGFONT lf;
162 wxZeroMemory(lf); // all default values
163
373a5fb3 164 lf.lfCharSet = (BYTE)info.charset;
98a52819 165 wxStrncpy(lf.lfFaceName, info.facename, WXSIZEOF(lf.lfFaceName));
11c7d5b6
VZ
166
167 HFONT hfont = ::CreateFontIndirect(&lf);
168 if ( !hfont )
169 {
170 // no such font
cbe874bd 171 return false;
11c7d5b6
VZ
172 }
173
174 ::DeleteObject((HGDIOBJ)hfont);
175
cbe874bd 176 return true;
11c7d5b6
VZ
177}
178
f6bcfd97
BP
179// ----------------------------------------------------------------------------
180// wxFontEncoding <-> CHARSET_XXX
181// ----------------------------------------------------------------------------
182
183wxFontEncoding wxGetFontEncFromCharSet(int cs)
184{
185 wxFontEncoding fontEncoding;
186
187 switch ( cs )
188 {
189 default:
b19dae2c
VZ
190 wxFAIL_MSG( _T("unexpected Win32 charset") );
191 // fall through and assume the system charset
192
193 case DEFAULT_CHARSET:
4ec6efd6
VZ
194 fontEncoding = wxFONTENCODING_SYSTEM;
195 break;
f6bcfd97
BP
196
197 case ANSI_CHARSET:
198 fontEncoding = wxFONTENCODING_CP1252;
199 break;
200
b19dae2c
VZ
201 case SYMBOL_CHARSET:
202 // what can we do here?
203 fontEncoding = wxFONTENCODING_MAX;
204 break;
205
04ef50df 206#if defined(__WIN32__) && !defined(__WXMICROWIN__)
f6bcfd97
BP
207 case EASTEUROPE_CHARSET:
208 fontEncoding = wxFONTENCODING_CP1250;
209 break;
210
211 case BALTIC_CHARSET:
212 fontEncoding = wxFONTENCODING_CP1257;
213 break;
214
215 case RUSSIAN_CHARSET:
216 fontEncoding = wxFONTENCODING_CP1251;
217 break;
218
219 case ARABIC_CHARSET:
220 fontEncoding = wxFONTENCODING_CP1256;
221 break;
222
223 case GREEK_CHARSET:
224 fontEncoding = wxFONTENCODING_CP1253;
225 break;
226
227 case HEBREW_CHARSET:
228 fontEncoding = wxFONTENCODING_CP1255;
229 break;
230
231 case TURKISH_CHARSET:
232 fontEncoding = wxFONTENCODING_CP1254;
233 break;
234
235 case THAI_CHARSET:
3c832d58 236 fontEncoding = wxFONTENCODING_CP874;
f6bcfd97 237 break;
bc4e6fcd
VZ
238
239 case SHIFTJIS_CHARSET:
240 fontEncoding = wxFONTENCODING_CP932;
241 break;
242
243 case GB2312_CHARSET:
244 fontEncoding = wxFONTENCODING_CP936;
245 break;
246
247 case HANGUL_CHARSET:
248 fontEncoding = wxFONTENCODING_CP949;
249 break;
250
251 case CHINESEBIG5_CHARSET:
252 fontEncoding = wxFONTENCODING_CP950;
253 break;
254
f6bcfd97
BP
255#endif // Win32
256
257 case OEM_CHARSET:
258 fontEncoding = wxFONTENCODING_CP437;
259 break;
260 }
261
262 return fontEncoding;
263}
264
11c7d5b6
VZ
265// ----------------------------------------------------------------------------
266// wxFont <-> LOGFONT conversion
267// ----------------------------------------------------------------------------
268
269void wxFillLogFont(LOGFONT *logFont, const wxFont *font)
270{
3bf5a59b
VZ
271 wxNativeFontInfo fi;
272
7936354d 273 // maybe we already have LOGFONT for this font?
3bf5a59b
VZ
274 const wxNativeFontInfo *pFI = font->GetNativeFontInfo();
275 if ( !pFI )
11c7d5b6 276 {
7936354d 277 // use wxNativeFontInfo methods to build a LOGFONT for this font
3bf5a59b 278 fi.InitFromFont(*font);
11c7d5b6 279
3bf5a59b 280 pFI = &fi;
11c7d5b6
VZ
281 }
282
283 // transfer all the data to LOGFONT
3bf5a59b 284 *logFont = pFI->lf;
11c7d5b6
VZ
285}
286
287wxFont wxCreateFontFromLogFont(const LOGFONT *logFont)
288{
09fcd889 289 wxNativeFontInfo info;
11c7d5b6 290
09fcd889 291 info.lf = *logFont;
11c7d5b6 292
09fcd889 293 return wxFont(info);
11c7d5b6
VZ
294}
295