]>
Commit | Line | Data |
---|---|---|
e2478fde VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: common/fmapbase.cpp | |
3 | // Purpose: wxFontMapperBase class implementation | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 21.06.2003 (extracted from common/fontmap.cpp) | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1999-2003 Vadim Zeitlin <vadim@wxwindows.org> | |
0a53b9b8 | 9 | // License: wxWindows license |
e2478fde VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
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 | #if wxUSE_FONTMAP | |
28 | ||
29 | #ifndef WX_PRECOMP | |
30 | #include "wx/app.h" | |
31 | #include "wx/log.h" | |
32 | #include "wx/intl.h" | |
33 | #endif //WX_PRECOMP | |
34 | ||
1c193821 JS |
35 | #if defined(__WXMSW__) |
36 | #include "wx/msw/private.h" // includes windows.h for LOGFONT | |
37 | #include "wx/msw/winundef.h" | |
38 | #endif | |
39 | ||
e2478fde VZ |
40 | #include "wx/fontmap.h" |
41 | #include "wx/fmappriv.h" | |
42 | ||
43 | #include "wx/apptrait.h" | |
44 | #include "wx/module.h" | |
45 | ||
4676948b JS |
46 | // wxMemoryConfig uses wxFileConfig |
47 | #if wxUSE_CONFIG && wxUSE_FILECONFIG | |
e2478fde VZ |
48 | #include "wx/config.h" |
49 | #include "wx/memconf.h" | |
50 | #endif | |
51 | ||
52 | // ---------------------------------------------------------------------------- | |
53 | // constants | |
54 | // ---------------------------------------------------------------------------- | |
55 | ||
56 | // encodings supported by GetEncodingDescription | |
57 | static wxFontEncoding gs_encodings[] = | |
58 | { | |
59 | wxFONTENCODING_ISO8859_1, | |
60 | wxFONTENCODING_ISO8859_2, | |
61 | wxFONTENCODING_ISO8859_3, | |
62 | wxFONTENCODING_ISO8859_4, | |
63 | wxFONTENCODING_ISO8859_5, | |
64 | wxFONTENCODING_ISO8859_6, | |
65 | wxFONTENCODING_ISO8859_7, | |
66 | wxFONTENCODING_ISO8859_8, | |
67 | wxFONTENCODING_ISO8859_9, | |
68 | wxFONTENCODING_ISO8859_10, | |
69 | wxFONTENCODING_ISO8859_11, | |
70 | wxFONTENCODING_ISO8859_12, | |
71 | wxFONTENCODING_ISO8859_13, | |
72 | wxFONTENCODING_ISO8859_14, | |
73 | wxFONTENCODING_ISO8859_15, | |
74 | wxFONTENCODING_KOI8, | |
15ad38c3 | 75 | wxFONTENCODING_KOI8_U, |
3c832d58 | 76 | wxFONTENCODING_CP874, |
e2478fde VZ |
77 | wxFONTENCODING_CP932, |
78 | wxFONTENCODING_CP936, | |
79 | wxFONTENCODING_CP949, | |
80 | wxFONTENCODING_CP950, | |
81 | wxFONTENCODING_CP1250, | |
82 | wxFONTENCODING_CP1251, | |
83 | wxFONTENCODING_CP1252, | |
84 | wxFONTENCODING_CP1253, | |
85 | wxFONTENCODING_CP1254, | |
86 | wxFONTENCODING_CP1255, | |
87 | wxFONTENCODING_CP1256, | |
88 | wxFONTENCODING_CP1257, | |
89 | wxFONTENCODING_CP437, | |
90 | wxFONTENCODING_UTF7, | |
91 | wxFONTENCODING_UTF8, | |
c91830cb VZ |
92 | wxFONTENCODING_UTF16, |
93 | wxFONTENCODING_UTF16BE, | |
94 | wxFONTENCODING_UTF16LE, | |
95 | wxFONTENCODING_UTF32, | |
96 | wxFONTENCODING_UTF32BE, | |
97 | wxFONTENCODING_UTF32LE, | |
e2478fde VZ |
98 | wxFONTENCODING_EUC_JP, |
99 | }; | |
100 | ||
101 | // the descriptions for them | |
102 | static const wxChar* gs_encodingDescs[] = | |
103 | { | |
104 | wxTRANSLATE( "Western European (ISO-8859-1)" ), | |
105 | wxTRANSLATE( "Central European (ISO-8859-2)" ), | |
106 | wxTRANSLATE( "Esperanto (ISO-8859-3)" ), | |
107 | wxTRANSLATE( "Baltic (old) (ISO-8859-4)" ), | |
108 | wxTRANSLATE( "Cyrillic (ISO-8859-5)" ), | |
109 | wxTRANSLATE( "Arabic (ISO-8859-6)" ), | |
110 | wxTRANSLATE( "Greek (ISO-8859-7)" ), | |
111 | wxTRANSLATE( "Hebrew (ISO-8859-8)" ), | |
112 | wxTRANSLATE( "Turkish (ISO-8859-9)" ), | |
113 | wxTRANSLATE( "Nordic (ISO-8859-10)" ), | |
114 | wxTRANSLATE( "Thai (ISO-8859-11)" ), | |
115 | wxTRANSLATE( "Indian (ISO-8859-12)" ), | |
116 | wxTRANSLATE( "Baltic (ISO-8859-13)" ), | |
117 | wxTRANSLATE( "Celtic (ISO-8859-14)" ), | |
118 | wxTRANSLATE( "Western European with Euro (ISO-8859-15)" ), | |
119 | wxTRANSLATE( "KOI8-R" ), | |
15ad38c3 | 120 | wxTRANSLATE( "KOI8-U" ), |
3c832d58 | 121 | wxTRANSLATE( "Windows Thai (CP 874)" ), |
e2478fde VZ |
122 | wxTRANSLATE( "Windows Japanese (CP 932)" ), |
123 | wxTRANSLATE( "Windows Chinese Simplified (CP 936)" ), | |
124 | wxTRANSLATE( "Windows Korean (CP 949)" ), | |
125 | wxTRANSLATE( "Windows Chinese Traditional (CP 950)" ), | |
126 | wxTRANSLATE( "Windows Central European (CP 1250)" ), | |
127 | wxTRANSLATE( "Windows Cyrillic (CP 1251)" ), | |
128 | wxTRANSLATE( "Windows Western European (CP 1252)" ), | |
129 | wxTRANSLATE( "Windows Greek (CP 1253)" ), | |
130 | wxTRANSLATE( "Windows Turkish (CP 1254)" ), | |
131 | wxTRANSLATE( "Windows Hebrew (CP 1255)" ), | |
132 | wxTRANSLATE( "Windows Arabic (CP 1256)" ), | |
133 | wxTRANSLATE( "Windows Baltic (CP 1257)" ), | |
134 | wxTRANSLATE( "Windows/DOS OEM (CP 437)" ), | |
135 | wxTRANSLATE( "Unicode 7 bit (UTF-7)" ), | |
136 | wxTRANSLATE( "Unicode 8 bit (UTF-8)" ), | |
c91830cb VZ |
137 | wxTRANSLATE( "Unicode 16 bit (UTF-16)" ), |
138 | wxTRANSLATE( "Unicode 16 bit Big Endian (UTF-16BE)" ), | |
139 | wxTRANSLATE( "Unicode 16 bit Little Endian (UTF-16LE)" ), | |
140 | wxTRANSLATE( "Unicode 32 bit (UTF-32)" ), | |
141 | wxTRANSLATE( "Unicode 32 bit Big Endian (UTF-32BE)" ), | |
142 | wxTRANSLATE( "Unicode 32 bit Little Endian (UTF-32LE)" ), | |
e2478fde VZ |
143 | wxTRANSLATE( "Extended Unix Codepage for Japanese (EUC-JP)" ), |
144 | }; | |
145 | ||
146 | // and the internal names (these are not translated on purpose!) | |
147 | static const wxChar* gs_encodingNames[] = | |
148 | { | |
149 | wxT( "iso-8859-1" ), | |
150 | wxT( "iso-8859-2" ), | |
151 | wxT( "iso-8859-3" ), | |
152 | wxT( "iso-8859-4" ), | |
153 | wxT( "iso-8859-5" ), | |
154 | wxT( "iso-8859-6" ), | |
155 | wxT( "iso-8859-7" ), | |
156 | wxT( "iso-8859-8" ), | |
157 | wxT( "iso-8859-9" ), | |
158 | wxT( "iso-8859-10" ), | |
159 | wxT( "iso-8859-11" ), | |
160 | wxT( "iso-8859-12" ), | |
161 | wxT( "iso-8859-13" ), | |
162 | wxT( "iso-8859-14" ), | |
163 | wxT( "iso-8859-15" ), | |
164 | wxT( "koi8-r" ), | |
15ad38c3 | 165 | wxT( "koi8-u" ), |
3c832d58 | 166 | wxT( "windows-874" ), |
e2478fde VZ |
167 | wxT( "windows-932" ), |
168 | wxT( "windows-936" ), | |
169 | wxT( "windows-949" ), | |
170 | wxT( "windows-950" ), | |
171 | wxT( "windows-1250" ), | |
172 | wxT( "windows-1251" ), | |
173 | wxT( "windows-1252" ), | |
174 | wxT( "windows-1253" ), | |
175 | wxT( "windows-1254" ), | |
176 | wxT( "windows-1255" ), | |
177 | wxT( "windows-1256" ), | |
178 | wxT( "windows-1257" ), | |
179 | wxT( "windows-437" ), | |
180 | wxT( "utf-7" ), | |
181 | wxT( "utf-8" ), | |
c91830cb VZ |
182 | wxT( "utf-16" ), |
183 | wxT( "utf-16be" ), | |
184 | wxT( "utf-16le" ), | |
185 | wxT( "utf-32" ), | |
186 | wxT( "utf-32be" ), | |
187 | wxT( "utf-32le" ), | |
e2478fde VZ |
188 | wxT( "euc-jp" ), |
189 | }; | |
190 | ||
191 | wxCOMPILE_TIME_ASSERT( WXSIZEOF(gs_encodingDescs) == WXSIZEOF(gs_encodings) && | |
192 | WXSIZEOF(gs_encodingNames) == WXSIZEOF(gs_encodings), | |
193 | EncodingsArraysNotInSync ); | |
194 | ||
195 | // ---------------------------------------------------------------------------- | |
196 | // private classes | |
197 | // ---------------------------------------------------------------------------- | |
198 | ||
199 | // clean up the font mapper object | |
200 | class wxFontMapperModule : public wxModule | |
201 | { | |
202 | public: | |
203 | wxFontMapperModule() : wxModule() { } | |
a62848fd | 204 | virtual bool OnInit() { return true; } |
2a12cc65 | 205 | virtual void OnExit() { delete (wxFontMapperBase*)wxFontMapperBase::Set(NULL); } |
e2478fde VZ |
206 | |
207 | DECLARE_DYNAMIC_CLASS(wxFontMapperModule) | |
208 | }; | |
209 | ||
210 | IMPLEMENT_DYNAMIC_CLASS(wxFontMapperModule, wxModule) | |
211 | ||
212 | ||
213 | // ============================================================================ | |
214 | // wxFontMapperBase implementation | |
215 | // ============================================================================ | |
216 | ||
217 | wxFontMapper *wxFontMapperBase::sm_instance = NULL; | |
218 | ||
219 | // ---------------------------------------------------------------------------- | |
220 | // ctor and dtor | |
221 | // ---------------------------------------------------------------------------- | |
222 | ||
223 | wxFontMapperBase::wxFontMapperBase() | |
224 | { | |
4676948b | 225 | #if wxUSE_CONFIG && wxUSE_FILECONFIG |
e2478fde | 226 | m_config = NULL; |
a62848fd | 227 | m_configIsDummy = false; |
e2478fde VZ |
228 | #endif // wxUSE_CONFIG |
229 | } | |
230 | ||
231 | wxFontMapperBase::~wxFontMapperBase() | |
232 | { | |
4676948b | 233 | #if wxUSE_CONFIG && wxUSE_FILECONFIG |
e2478fde VZ |
234 | if ( m_configIsDummy ) |
235 | delete m_config; | |
236 | #endif // wxUSE_CONFIG | |
237 | } | |
238 | ||
2a12cc65 DE |
239 | bool wxFontMapperBase::IsWxFontMapper() |
240 | { return false; } | |
241 | ||
e2478fde | 242 | /* static */ |
73302af6 | 243 | wxFontMapperBase *wxFontMapperBase::Get() |
e2478fde VZ |
244 | { |
245 | if ( !sm_instance ) | |
246 | { | |
247 | wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL; | |
248 | if ( traits ) | |
249 | { | |
250 | sm_instance = traits->CreateFontMapper(); | |
251 | ||
252 | wxASSERT_MSG( sm_instance, | |
253 | _T("wxAppTraits::CreateFontMapper() failed") ); | |
254 | } | |
255 | ||
256 | if ( !sm_instance ) | |
257 | { | |
258 | // last resort: we must create something because the existing code | |
259 | // relies on always having a valid font mapper object | |
260 | sm_instance = (wxFontMapper *)new wxFontMapperBase; | |
261 | } | |
262 | } | |
263 | ||
73302af6 | 264 | return (wxFontMapperBase*)sm_instance; |
e2478fde VZ |
265 | } |
266 | ||
267 | /* static */ | |
268 | wxFontMapper *wxFontMapperBase::Set(wxFontMapper *mapper) | |
269 | { | |
270 | wxFontMapper *old = sm_instance; | |
271 | sm_instance = mapper; | |
272 | return old; | |
273 | } | |
274 | ||
4676948b | 275 | #if wxUSE_CONFIG && wxUSE_FILECONFIG |
e2478fde VZ |
276 | |
277 | // ---------------------------------------------------------------------------- | |
278 | // config usage customisation | |
279 | // ---------------------------------------------------------------------------- | |
280 | ||
281 | /* static */ | |
282 | const wxChar *wxFontMapperBase::GetDefaultConfigPath() | |
283 | { | |
284 | return FONTMAPPER_ROOT_PATH; | |
285 | } | |
286 | ||
287 | void wxFontMapperBase::SetConfigPath(const wxString& prefix) | |
288 | { | |
289 | wxCHECK_RET( !prefix.IsEmpty() && prefix[0] == wxCONFIG_PATH_SEPARATOR, | |
290 | wxT("an absolute path should be given to wxFontMapper::SetConfigPath()") ); | |
291 | ||
292 | m_configRootPath = prefix; | |
293 | } | |
294 | ||
295 | // ---------------------------------------------------------------------------- | |
296 | // get config object and path for it | |
297 | // ---------------------------------------------------------------------------- | |
298 | ||
299 | wxConfigBase *wxFontMapperBase::GetConfig() | |
300 | { | |
301 | if ( !m_config ) | |
302 | { | |
303 | // try the default | |
a62848fd | 304 | m_config = wxConfig::Get(false /*don't create on demand*/ ); |
e2478fde VZ |
305 | |
306 | if ( !m_config ) | |
307 | { | |
308 | // we still want to have a config object because otherwise we would | |
309 | // keep asking the user the same questions in the interactive mode, | |
310 | // so create a dummy config which won't write to any files/registry | |
311 | // but will allow us to remember the results of the questions at | |
312 | // least during this run | |
313 | m_config = new wxMemoryConfig; | |
a62848fd | 314 | m_configIsDummy = true; |
e2478fde VZ |
315 | // VS: we can't call wxConfig::Set(m_config) here because that would |
316 | // disable automatic wxConfig instance creation if this code was | |
317 | // called before wxApp::OnInit (this happens in wxGTK -- it sets | |
318 | // default wxFont encoding in wxApp::Initialize()) | |
319 | } | |
320 | } | |
321 | ||
a62848fd | 322 | if ( m_configIsDummy && wxConfig::Get(false) != NULL ) |
e2478fde VZ |
323 | { |
324 | // VS: in case we created dummy m_config (see above), we want to switch back | |
325 | // to the real one as soon as one becomes available. | |
326 | delete m_config; | |
a62848fd WS |
327 | m_config = wxConfig::Get(false); |
328 | m_configIsDummy = false; | |
e2478fde VZ |
329 | // FIXME: ideally, we should add keys from dummy config to the real one now, |
330 | // but it is a low-priority task because typical wxWin application | |
331 | // either doesn't use wxConfig at all or creates wxConfig object in | |
332 | // wxApp::OnInit(), before any real interaction with the user takes | |
333 | // place... | |
334 | } | |
335 | ||
336 | return m_config; | |
337 | } | |
338 | ||
339 | const wxString& wxFontMapperBase::GetConfigPath() | |
340 | { | |
341 | if ( !m_configRootPath ) | |
342 | { | |
343 | // use the default | |
344 | m_configRootPath = GetDefaultConfigPath(); | |
345 | } | |
346 | ||
347 | return m_configRootPath; | |
348 | } | |
349 | ||
350 | // ---------------------------------------------------------------------------- | |
351 | // config helpers | |
352 | // ---------------------------------------------------------------------------- | |
353 | ||
354 | bool wxFontMapperBase::ChangePath(const wxString& pathNew, wxString *pathOld) | |
355 | { | |
356 | wxConfigBase *config = GetConfig(); | |
357 | if ( !config ) | |
a62848fd | 358 | return false; |
e2478fde VZ |
359 | |
360 | *pathOld = config->GetPath(); | |
361 | ||
362 | wxString path = GetConfigPath(); | |
363 | if ( path.IsEmpty() || path.Last() != wxCONFIG_PATH_SEPARATOR ) | |
364 | { | |
365 | path += wxCONFIG_PATH_SEPARATOR; | |
366 | } | |
367 | ||
368 | wxASSERT_MSG( !pathNew || (pathNew[0] != wxCONFIG_PATH_SEPARATOR), | |
369 | wxT("should be a relative path") ); | |
370 | ||
371 | path += pathNew; | |
372 | ||
373 | config->SetPath(path); | |
374 | ||
a62848fd | 375 | return true; |
e2478fde VZ |
376 | } |
377 | ||
378 | void wxFontMapperBase::RestorePath(const wxString& pathOld) | |
379 | { | |
380 | GetConfig()->SetPath(pathOld); | |
381 | } | |
382 | ||
f1c75e0f JS |
383 | #endif |
384 | ||
e2478fde VZ |
385 | // ---------------------------------------------------------------------------- |
386 | // charset/encoding correspondence | |
387 | // ---------------------------------------------------------------------------- | |
388 | ||
389 | wxFontEncoding | |
390 | wxFontMapperBase::CharsetToEncoding(const wxString& charset, | |
391 | bool WXUNUSED(interactive)) | |
4dc55027 VZ |
392 | { |
393 | int enc = NonInteractiveCharsetToEncoding(charset); | |
394 | if ( enc == wxFONTENCODING_UNKNOWN ) | |
395 | { | |
396 | // we should return wxFONTENCODING_SYSTEM from here for unknown | |
397 | // encodings | |
398 | enc = wxFONTENCODING_SYSTEM; | |
399 | } | |
400 | ||
401 | return (wxFontEncoding)enc; | |
402 | } | |
403 | ||
404 | int | |
405 | wxFontMapperBase::NonInteractiveCharsetToEncoding(const wxString& charset) | |
e2478fde VZ |
406 | { |
407 | wxFontEncoding encoding = wxFONTENCODING_SYSTEM; | |
408 | ||
409 | // we're going to modify it, make a copy | |
410 | wxString cs = charset; | |
411 | ||
4676948b | 412 | #if wxUSE_CONFIG && wxUSE_FILECONFIG |
e2478fde VZ |
413 | // first try the user-defined settings |
414 | wxFontMapperPathChanger path(this, FONTMAPPER_CHARSET_PATH); | |
415 | if ( path.IsOk() ) | |
416 | { | |
417 | wxConfigBase *config = GetConfig(); | |
418 | ||
419 | // do we have an encoding for this charset? | |
420 | long value = config->Read(charset, -1l); | |
421 | if ( value != -1 ) | |
422 | { | |
423 | if ( value == wxFONTENCODING_UNKNOWN ) | |
424 | { | |
425 | // don't try to find it, in particular don't ask the user | |
4dc55027 | 426 | return value; |
e2478fde VZ |
427 | } |
428 | ||
429 | if ( value >= 0 && value <= wxFONTENCODING_MAX ) | |
430 | { | |
431 | encoding = (wxFontEncoding)value; | |
432 | } | |
433 | else | |
434 | { | |
435 | wxLogDebug(wxT("corrupted config data: invalid encoding %ld for charset '%s' ignored"), | |
436 | value, charset.c_str()); | |
437 | } | |
438 | } | |
439 | ||
440 | if ( encoding == wxFONTENCODING_SYSTEM ) | |
441 | { | |
442 | // may be we have an alias? | |
443 | config->SetPath(FONTMAPPER_CHARSET_ALIAS_PATH); | |
444 | ||
445 | wxString alias = config->Read(charset); | |
a62848fd | 446 | if ( !alias.IsEmpty() ) |
e2478fde VZ |
447 | { |
448 | // yes, we do - use it instead | |
449 | cs = alias; | |
450 | } | |
451 | } | |
452 | } | |
453 | #endif // wxUSE_CONFIG | |
454 | ||
455 | // if didn't find it there, try to recognize it ourselves | |
456 | if ( encoding == wxFONTENCODING_SYSTEM ) | |
457 | { | |
458 | // trim any spaces | |
459 | cs.Trim(true); | |
460 | cs.Trim(false); | |
461 | ||
462 | // discard the optional quotes | |
463 | if ( !cs.empty() ) | |
464 | { | |
465 | if ( cs[0u] == _T('"') && cs.Last() == _T('"') ) | |
466 | { | |
467 | cs = wxString(cs.c_str(), cs.length() - 1); | |
468 | } | |
469 | } | |
470 | ||
471 | cs.MakeUpper(); | |
472 | ||
473 | if ( cs.empty() || cs == _T("US-ASCII") ) | |
474 | { | |
475 | encoding = wxFONTENCODING_DEFAULT; | |
476 | } | |
477 | else if ( cs == wxT("UTF-7") ) | |
478 | { | |
479 | encoding = wxFONTENCODING_UTF7; | |
480 | } | |
481 | else if ( cs == wxT("UTF-8") ) | |
482 | { | |
483 | encoding = wxFONTENCODING_UTF8; | |
484 | } | |
c91830cb VZ |
485 | else if ( cs == wxT("UTF-16") ) |
486 | { | |
487 | encoding = wxFONTENCODING_UTF16; | |
488 | } | |
489 | else if ( cs == wxT("UTF-16BE") ) | |
490 | { | |
491 | encoding = wxFONTENCODING_UTF16BE; | |
492 | } | |
493 | else if ( cs == wxT("UTF-16LE") ) | |
494 | { | |
495 | encoding = wxFONTENCODING_UTF16LE; | |
496 | } | |
497 | else if ( cs == wxT("UTF-32") || cs == wxT("UCS-4") ) | |
498 | { | |
499 | encoding = wxFONTENCODING_UTF32; | |
500 | } | |
501 | else if ( cs == wxT("UTF-32BE") || cs == wxT("UCS-4BE") ) | |
502 | { | |
503 | encoding = wxFONTENCODING_UTF32BE; | |
504 | } | |
505 | else if ( cs == wxT("UTF-32LE") || cs == wxT("UCS-4LE") ) | |
506 | { | |
507 | encoding = wxFONTENCODING_UTF32LE; | |
508 | } | |
e2478fde VZ |
509 | else if ( cs == wxT("GB2312") ) |
510 | { | |
511 | encoding = wxFONTENCODING_GB2312; | |
512 | } | |
513 | else if ( cs == wxT("BIG5") ) | |
514 | { | |
515 | encoding = wxFONTENCODING_BIG5; | |
516 | } | |
517 | else if ( cs == wxT("SJIS") || | |
518 | cs == wxT("SHIFT_JIS") || | |
519 | cs == wxT("SHIFT-JIS") ) | |
520 | { | |
521 | encoding = wxFONTENCODING_SHIFT_JIS; | |
522 | } | |
523 | else if ( cs == wxT("EUC-JP") || | |
524 | cs == wxT("EUC_JP") ) | |
525 | { | |
526 | encoding = wxFONTENCODING_EUC_JP; | |
527 | } | |
51a4142c VZ |
528 | else if ( cs == wxT("EUC-KR") || |
529 | cs == wxT("EUC_KR") ) | |
530 | { | |
531 | encoding = wxFONTENCODING_CP949; | |
a62848fd | 532 | } |
e2478fde | 533 | else if ( cs == wxT("KOI8-R") || |
e2478fde VZ |
534 | cs == wxT("KOI8-RU") ) |
535 | { | |
536 | // although koi8-ru is not strictly speaking the same as koi8-r, | |
537 | // they are similar enough to make mapping it to koi8 better than | |
15ad38c3 | 538 | // not recognizing it at all |
e2478fde VZ |
539 | encoding = wxFONTENCODING_KOI8; |
540 | } | |
15ad38c3 VZ |
541 | else if ( cs == wxT("KOI8-U") ) |
542 | { | |
543 | encoding = wxFONTENCODING_KOI8_U; | |
544 | } | |
e2478fde VZ |
545 | else if ( cs.Left(3) == wxT("ISO") ) |
546 | { | |
547 | // the dash is optional (or, to be exact, it is not, but | |
548 | // several brokenmails "forget" it) | |
549 | const wxChar *p = cs.c_str() + 3; | |
550 | if ( *p == wxT('-') ) | |
551 | p++; | |
a62848fd | 552 | |
e2478fde VZ |
553 | // printf( "iso %s\n", (const char*) cs.ToAscii() ); |
554 | ||
555 | unsigned int value; | |
556 | if ( wxSscanf(p, wxT("8859-%u"), &value) == 1 ) | |
557 | { | |
558 | // printf( "value %d\n", (int)value ); | |
a62848fd | 559 | |
e2478fde VZ |
560 | // make it 0 based and check that it is strictly positive in |
561 | // the process (no such thing as iso8859-0 encoding) | |
562 | if ( (value-- > 0) && | |
563 | (value < wxFONTENCODING_ISO8859_MAX - | |
564 | wxFONTENCODING_ISO8859_1) ) | |
565 | { | |
566 | // it's a valid ISO8859 encoding | |
567 | value += wxFONTENCODING_ISO8859_1; | |
568 | encoding = (wxFontEncoding)value; | |
569 | } | |
570 | } | |
571 | } | |
572 | else if ( cs.Left(4) == wxT("8859") ) | |
573 | { | |
574 | const wxChar *p = cs.c_str(); | |
a62848fd | 575 | |
e2478fde VZ |
576 | unsigned int value; |
577 | if ( wxSscanf(p, wxT("8859-%u"), &value) == 1 ) | |
578 | { | |
579 | // printf( "value %d\n", (int)value ); | |
a62848fd | 580 | |
e2478fde VZ |
581 | // make it 0 based and check that it is strictly positive in |
582 | // the process (no such thing as iso8859-0 encoding) | |
583 | if ( (value-- > 0) && | |
584 | (value < wxFONTENCODING_ISO8859_MAX - | |
585 | wxFONTENCODING_ISO8859_1) ) | |
586 | { | |
587 | // it's a valid ISO8859 encoding | |
588 | value += wxFONTENCODING_ISO8859_1; | |
589 | encoding = (wxFontEncoding)value; | |
590 | } | |
591 | } | |
592 | } | |
593 | else // check for Windows charsets | |
594 | { | |
595 | size_t len; | |
596 | if ( cs.Left(7) == wxT("WINDOWS") ) | |
597 | { | |
598 | len = 7; | |
599 | } | |
600 | else if ( cs.Left(2) == wxT("CP") ) | |
601 | { | |
602 | len = 2; | |
603 | } | |
604 | else // not a Windows encoding | |
605 | { | |
606 | len = 0; | |
607 | } | |
608 | ||
609 | if ( len ) | |
610 | { | |
611 | const wxChar *p = cs.c_str() + len; | |
612 | if ( *p == wxT('-') ) | |
613 | p++; | |
614 | ||
615 | int value; | |
616 | if ( wxSscanf(p, wxT("%u"), &value) == 1 ) | |
617 | { | |
618 | if ( value >= 1250 ) | |
619 | { | |
620 | value -= 1250; | |
621 | if ( value < wxFONTENCODING_CP12_MAX - | |
622 | wxFONTENCODING_CP1250 ) | |
623 | { | |
624 | // a valid Windows code page | |
625 | value += wxFONTENCODING_CP1250; | |
626 | encoding = (wxFontEncoding)value; | |
627 | } | |
628 | } | |
629 | ||
630 | switch ( value ) | |
631 | { | |
3c832d58 DS |
632 | case 874: |
633 | encoding = wxFONTENCODING_CP874; | |
634 | break; | |
635 | ||
e2478fde VZ |
636 | case 932: |
637 | encoding = wxFONTENCODING_CP932; | |
638 | break; | |
639 | ||
640 | case 936: | |
641 | encoding = wxFONTENCODING_CP936; | |
642 | break; | |
643 | ||
644 | case 949: | |
645 | encoding = wxFONTENCODING_CP949; | |
646 | break; | |
647 | ||
648 | case 950: | |
649 | encoding = wxFONTENCODING_CP950; | |
650 | break; | |
651 | } | |
652 | } | |
653 | } | |
654 | } | |
655 | //else: unknown | |
656 | } | |
657 | ||
658 | return encoding; | |
659 | } | |
660 | ||
661 | /* static */ | |
662 | size_t wxFontMapperBase::GetSupportedEncodingsCount() | |
663 | { | |
664 | return WXSIZEOF(gs_encodings); | |
665 | } | |
666 | ||
667 | /* static */ | |
668 | wxFontEncoding wxFontMapperBase::GetEncoding(size_t n) | |
669 | { | |
670 | wxCHECK_MSG( n < WXSIZEOF(gs_encodings), wxFONTENCODING_SYSTEM, | |
671 | _T("wxFontMapper::GetEncoding(): invalid index") ); | |
672 | ||
673 | return gs_encodings[n]; | |
674 | } | |
675 | ||
676 | /* static */ | |
677 | wxString wxFontMapperBase::GetEncodingDescription(wxFontEncoding encoding) | |
678 | { | |
679 | if ( encoding == wxFONTENCODING_DEFAULT ) | |
680 | { | |
681 | return _("Default encoding"); | |
682 | } | |
683 | ||
684 | const size_t count = WXSIZEOF(gs_encodingDescs); | |
685 | ||
686 | for ( size_t i = 0; i < count; i++ ) | |
687 | { | |
688 | if ( gs_encodings[i] == encoding ) | |
689 | { | |
690 | return wxGetTranslation(gs_encodingDescs[i]); | |
691 | } | |
692 | } | |
693 | ||
694 | wxString str; | |
695 | str.Printf(_("Unknown encoding (%d)"), encoding); | |
696 | ||
697 | return str; | |
698 | } | |
699 | ||
700 | /* static */ | |
701 | wxString wxFontMapperBase::GetEncodingName(wxFontEncoding encoding) | |
702 | { | |
703 | if ( encoding == wxFONTENCODING_DEFAULT ) | |
704 | { | |
705 | return _("default"); | |
706 | } | |
707 | ||
708 | const size_t count = WXSIZEOF(gs_encodingNames); | |
709 | ||
710 | for ( size_t i = 0; i < count; i++ ) | |
711 | { | |
712 | if ( gs_encodings[i] == encoding ) | |
713 | { | |
714 | return gs_encodingNames[i]; | |
715 | } | |
716 | } | |
717 | ||
718 | wxString str; | |
719 | str.Printf(_("unknown-%d"), encoding); | |
720 | ||
721 | return str; | |
722 | } | |
723 | ||
910b9fc5 VZ |
724 | /* static */ |
725 | wxFontEncoding wxFontMapperBase::GetEncodingFromName(const wxString& name) | |
726 | { | |
727 | const size_t count = WXSIZEOF(gs_encodingNames); | |
728 | ||
729 | for ( size_t i = 0; i < count; i++ ) | |
730 | { | |
731 | if ( gs_encodingNames[i] == name ) | |
732 | { | |
733 | return gs_encodings[i]; | |
734 | } | |
735 | } | |
736 | ||
737 | if ( name == _("default") ) | |
738 | { | |
739 | return wxFONTENCODING_DEFAULT; | |
740 | } | |
741 | ||
742 | return wxFONTENCODING_MAX; | |
743 | } | |
744 | ||
e2478fde VZ |
745 | #endif // wxUSE_FONTMAP |
746 |