]> git.saurik.com Git - wxWidgets.git/blame - src/common/fmapbase.cpp
fixed wxMBConv_iconv::GetMBNul()
[wxWidgets.git] / src / common / fmapbase.cpp
CommitLineData
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
57static 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_UTF16BE,
93 wxFONTENCODING_UTF16LE,
c91830cb
VZ
94 wxFONTENCODING_UTF32BE,
95 wxFONTENCODING_UTF32LE,
e2478fde 96 wxFONTENCODING_EUC_JP,
8b3eb85d
VZ
97 wxFONTENCODING_DEFAULT,
98 wxFONTENCODING_BIG5,
99 wxFONTENCODING_SHIFT_JIS,
100 wxFONTENCODING_GB2312,
e2478fde
VZ
101};
102
103// the descriptions for them
104static const wxChar* gs_encodingDescs[] =
105{
106 wxTRANSLATE( "Western European (ISO-8859-1)" ),
107 wxTRANSLATE( "Central European (ISO-8859-2)" ),
108 wxTRANSLATE( "Esperanto (ISO-8859-3)" ),
109 wxTRANSLATE( "Baltic (old) (ISO-8859-4)" ),
110 wxTRANSLATE( "Cyrillic (ISO-8859-5)" ),
111 wxTRANSLATE( "Arabic (ISO-8859-6)" ),
112 wxTRANSLATE( "Greek (ISO-8859-7)" ),
113 wxTRANSLATE( "Hebrew (ISO-8859-8)" ),
114 wxTRANSLATE( "Turkish (ISO-8859-9)" ),
115 wxTRANSLATE( "Nordic (ISO-8859-10)" ),
116 wxTRANSLATE( "Thai (ISO-8859-11)" ),
117 wxTRANSLATE( "Indian (ISO-8859-12)" ),
118 wxTRANSLATE( "Baltic (ISO-8859-13)" ),
119 wxTRANSLATE( "Celtic (ISO-8859-14)" ),
120 wxTRANSLATE( "Western European with Euro (ISO-8859-15)" ),
121 wxTRANSLATE( "KOI8-R" ),
15ad38c3 122 wxTRANSLATE( "KOI8-U" ),
3c832d58 123 wxTRANSLATE( "Windows Thai (CP 874)" ),
e2478fde
VZ
124 wxTRANSLATE( "Windows Japanese (CP 932)" ),
125 wxTRANSLATE( "Windows Chinese Simplified (CP 936)" ),
126 wxTRANSLATE( "Windows Korean (CP 949)" ),
127 wxTRANSLATE( "Windows Chinese Traditional (CP 950)" ),
128 wxTRANSLATE( "Windows Central European (CP 1250)" ),
129 wxTRANSLATE( "Windows Cyrillic (CP 1251)" ),
130 wxTRANSLATE( "Windows Western European (CP 1252)" ),
131 wxTRANSLATE( "Windows Greek (CP 1253)" ),
132 wxTRANSLATE( "Windows Turkish (CP 1254)" ),
133 wxTRANSLATE( "Windows Hebrew (CP 1255)" ),
134 wxTRANSLATE( "Windows Arabic (CP 1256)" ),
135 wxTRANSLATE( "Windows Baltic (CP 1257)" ),
136 wxTRANSLATE( "Windows/DOS OEM (CP 437)" ),
137 wxTRANSLATE( "Unicode 7 bit (UTF-7)" ),
138 wxTRANSLATE( "Unicode 8 bit (UTF-8)" ),
e4ccedf7 139#ifdef WORDS_BIGENDIAN
c91830cb 140 wxTRANSLATE( "Unicode 16 bit (UTF-16)" ),
c91830cb
VZ
141 wxTRANSLATE( "Unicode 16 bit Little Endian (UTF-16LE)" ),
142 wxTRANSLATE( "Unicode 32 bit (UTF-32)" ),
c91830cb 143 wxTRANSLATE( "Unicode 32 bit Little Endian (UTF-32LE)" ),
e4ccedf7
VZ
144#else // WORDS_BIGENDIAN
145 wxTRANSLATE( "Unicode 16 bit Big Endian (UTF-16BE)" ),
146 wxTRANSLATE( "Unicode 16 bit (UTF-16)" ),
147 wxTRANSLATE( "Unicode 32 bit Big Endian (UTF-32BE)" ),
148 wxTRANSLATE( "Unicode 32 bit (UTF-32)" ),
149#endif // WORDS_BIGENDIAN
e2478fde 150 wxTRANSLATE( "Extended Unix Codepage for Japanese (EUC-JP)" ),
8b3eb85d
VZ
151 wxTRANSLATE( "US-ASCII" ),
152 wxTRANSLATE( "BIG5" ),
153 wxTRANSLATE( "SHIFT-JIS" ),
154 wxTRANSLATE( "GB-2312" ),
e2478fde
VZ
155};
156
157// and the internal names (these are not translated on purpose!)
3f0785cd 158static const wxChar* gs_encodingNames[WXSIZEOF(gs_encodingDescs)][10] =
e2478fde 159{
e524af66
VZ
160 // names from the columns correspond to these OS:
161 // Linux Solaris and IRIX HP-UX AIX
162 { _T("ISO-8859-1"), _T("ISO8859-1"), _T("iso88591"), _T("8859-1"), wxT("iso_8859_1"), NULL },
163 { _T("ISO-8859-2"), _T("ISO8859-2"), _T("iso88592"), _T("8859-2"), NULL },
164 { _T("ISO-8859-3"), _T("ISO8859-3"), _T("iso88593"), _T("8859-3"), NULL },
165 { _T("ISO-8859-4"), _T("ISO8859-4"), _T("iso88594"), _T("8859-4"), NULL },
166 { _T("ISO-8859-5"), _T("ISO8859-5"), _T("iso88595"), _T("8859-5"), NULL },
167 { _T("ISO-8859-6"), _T("ISO8859-6"), _T("iso88596"), _T("8859-6"), NULL },
168 { _T("ISO-8859-7"), _T("ISO8859-7"), _T("iso88597"), _T("8859-7"), NULL },
169 { _T("ISO-8859-8"), _T("ISO8859-8"), _T("iso88598"), _T("8859-8"), NULL },
170 { _T("ISO-8859-9"), _T("ISO8859-9"), _T("iso88599"), _T("8859-9"), NULL },
171 { _T("ISO-8859-10"), _T("ISO8859-10"), _T("iso885910"), _T("8859-10"), NULL },
172 { _T("ISO-8859-11"), _T("ISO8859-11"), _T("iso885911"), _T("8859-11"), NULL },
173 { _T("ISO-8859-12"), _T("ISO8859-12"), _T("iso885912"), _T("8859-12"), NULL },
174 { _T("ISO-8859-13"), _T("ISO8859-13"), _T("iso885913"), _T("8859-13"), NULL },
175 { _T("ISO-8859-14"), _T("ISO8859-14"), _T("iso885914"), _T("8859-14"), NULL },
176 { _T("ISO-8859-15"), _T("ISO8859-15"), _T("iso885915"), _T("8859-15"), NULL },
761952d9 177
8b3eb85d
VZ
178 // although koi8-ru is not strictly speaking the same as koi8-r,
179 // they are similar enough to make mapping it to koi8 better than
180 // not recognizing it at all
761952d9
VZ
181 { wxT( "KOI8-R" ), wxT( "KOI8-RU" ), NULL },
182 { wxT( "KOI8-U" ), NULL },
183
3f0785cd
VZ
184 { wxT( "WINDOWS-874" ), wxT( "CP-874" ), NULL },
185 { wxT( "WINDOWS-932" ), wxT( "CP-932" ), NULL },
186 { wxT( "WINDOWS-936" ), wxT( "CP-936" ), NULL },
187 { wxT( "WINDOWS-949" ), wxT( "CP-949" ), wxT( "EUC-KR" ), wxT( "eucKR" ), wxT( "euc_kr" ), NULL },
188 { wxT( "WINDOWS-950" ), wxT( "CP-950" ), NULL },
189 { wxT( "WINDOWS-1250" ),wxT( "CP-1250" ), NULL },
190 { wxT( "WINDOWS-1251" ),wxT( "CP-1251" ), NULL },
191 { wxT( "WINDOWS-1252" ),wxT( "CP-1252" ), wxT("IBM-1252"), NULL },
192 { wxT( "WINDOWS-1253" ),wxT( "CP-1253" ), NULL },
193 { wxT( "WINDOWS-1254" ),wxT( "CP-1254" ), NULL },
194 { wxT( "WINDOWS-1255" ),wxT( "CP-1255" ), NULL },
195 { wxT( "WINDOWS-1256" ),wxT( "CP-1256" ), NULL },
196 { wxT( "WINDOWS-1257" ),wxT( "CP-1257" ), NULL },
197 { wxT( "WINDOWS-437" ), wxT( "CP-437" ), NULL },
761952d9 198
9ead8387
VZ
199 { wxT( "UTF-7" ), wxT("utf7"), NULL },
200 { wxT( "UTF-8" ), wxT("utf8"), NULL },
e4ccedf7
VZ
201#ifdef WORDS_BIGENDIAN
202 { wxT( "UTF-16BE" ), wxT("UCS-2BE"), wxT( "UTF-16" ), wxT("UCS-2"), wxT("UCS2"), NULL },
37d0e9f6 203 { wxT( "UTF-16LE" ), wxT("UCS-2LE"), NULL },
e4ccedf7 204 { wxT( "UTF-32BE" ), wxT( "UCS-4BE" ), wxT( "UTF-32" ), wxT( "UCS-4" ), wxT("UCS4"), NULL },
761952d9 205 { wxT( "UTF-32LE" ), wxT( "UCS-4LE" ), NULL },
e4ccedf7
VZ
206#else // WORDS_BIGENDIAN
207 { wxT( "UTF-16BE" ), wxT("UCS-2BE"), NULL },
208 { wxT( "UTF-16LE" ), wxT("UCS-2LE"), wxT( "UTF-16" ), wxT("UCS-2"), wxT("UCS2"), NULL },
209 { wxT( "UTF-32BE" ), wxT( "UCS-4BE" ), NULL },
210 { wxT( "UTF-32LE" ), wxT( "UCS-4LE" ), wxT( "UTF-32" ), wxT( "UCS-4" ), wxT("UCS4"), NULL },
211#endif // WORDS_BIGENDIAN
761952d9
VZ
212
213 { wxT( "EUC-JP" ), wxT( "eucJP" ), wxT( "euc_jp" ), wxT( "IBM-eucJP" ), NULL },
214
3f0785cd
VZ
215 // 646 is for Solaris, roman8 -- for HP-UX
216 { wxT( "US-ASCII" ), wxT( "ASCII" ), wxT("C"), wxT("POSIX"), wxT("ANSI_X3.4-1968"),
217 wxT("646"), wxT("roman8"), wxT( "" ), NULL },
218
9ead8387 219 { wxT( "BIG5" ), wxT("big5"), NULL },
cd5b511a 220 { wxT( "SJIS" ), wxT( "SHIFT-JIS" ), wxT( "SHIFT_JIS" ), NULL },
761952d9 221 { wxT( "GB2312" ), NULL },
e2478fde
VZ
222};
223
8b3eb85d
VZ
224wxCOMPILE_TIME_ASSERT( WXSIZEOF(gs_encodingDescs) == WXSIZEOF(gs_encodings), EncodingsArraysNotInSync );
225wxCOMPILE_TIME_ASSERT( WXSIZEOF(gs_encodingNames) == WXSIZEOF(gs_encodings), EncodingsArraysNotInSync );
e2478fde
VZ
226
227// ----------------------------------------------------------------------------
228// private classes
229// ----------------------------------------------------------------------------
230
231// clean up the font mapper object
232class wxFontMapperModule : public wxModule
233{
234public:
235 wxFontMapperModule() : wxModule() { }
a62848fd 236 virtual bool OnInit() { return true; }
2a12cc65 237 virtual void OnExit() { delete (wxFontMapperBase*)wxFontMapperBase::Set(NULL); }
e2478fde
VZ
238
239 DECLARE_DYNAMIC_CLASS(wxFontMapperModule)
240};
241
242IMPLEMENT_DYNAMIC_CLASS(wxFontMapperModule, wxModule)
243
244
245// ============================================================================
246// wxFontMapperBase implementation
247// ============================================================================
248
249wxFontMapper *wxFontMapperBase::sm_instance = NULL;
250
251// ----------------------------------------------------------------------------
252// ctor and dtor
253// ----------------------------------------------------------------------------
254
255wxFontMapperBase::wxFontMapperBase()
256{
4676948b 257#if wxUSE_CONFIG && wxUSE_FILECONFIG
5bbca8b0 258 m_configDummy = NULL;
e2478fde
VZ
259#endif // wxUSE_CONFIG
260}
261
262wxFontMapperBase::~wxFontMapperBase()
263{
4676948b 264#if wxUSE_CONFIG && wxUSE_FILECONFIG
5bbca8b0
KH
265 if ( m_configDummy )
266 delete m_configDummy;
e2478fde
VZ
267#endif // wxUSE_CONFIG
268}
269
2a12cc65
DE
270bool wxFontMapperBase::IsWxFontMapper()
271{ return false; }
272
e2478fde 273/* static */
73302af6 274wxFontMapperBase *wxFontMapperBase::Get()
e2478fde
VZ
275{
276 if ( !sm_instance )
277 {
278 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
279 if ( traits )
280 {
281 sm_instance = traits->CreateFontMapper();
282
283 wxASSERT_MSG( sm_instance,
284 _T("wxAppTraits::CreateFontMapper() failed") );
285 }
286
287 if ( !sm_instance )
288 {
289 // last resort: we must create something because the existing code
290 // relies on always having a valid font mapper object
291 sm_instance = (wxFontMapper *)new wxFontMapperBase;
292 }
293 }
294
73302af6 295 return (wxFontMapperBase*)sm_instance;
e2478fde
VZ
296}
297
298/* static */
299wxFontMapper *wxFontMapperBase::Set(wxFontMapper *mapper)
300{
301 wxFontMapper *old = sm_instance;
302 sm_instance = mapper;
303 return old;
304}
305
4676948b 306#if wxUSE_CONFIG && wxUSE_FILECONFIG
e2478fde
VZ
307
308// ----------------------------------------------------------------------------
309// config usage customisation
310// ----------------------------------------------------------------------------
311
312/* static */
313const wxChar *wxFontMapperBase::GetDefaultConfigPath()
314{
315 return FONTMAPPER_ROOT_PATH;
316}
317
318void wxFontMapperBase::SetConfigPath(const wxString& prefix)
319{
320 wxCHECK_RET( !prefix.IsEmpty() && prefix[0] == wxCONFIG_PATH_SEPARATOR,
321 wxT("an absolute path should be given to wxFontMapper::SetConfigPath()") );
322
323 m_configRootPath = prefix;
324}
325
326// ----------------------------------------------------------------------------
327// get config object and path for it
328// ----------------------------------------------------------------------------
329
330wxConfigBase *wxFontMapperBase::GetConfig()
331{
5bbca8b0 332 wxConfigBase *config = wxConfig::Get(false);
e2478fde 333
5bbca8b0
KH
334 // If there is no global configuration, use an internal memory configuration
335 if ( !config )
e2478fde 336 {
5bbca8b0
KH
337 if ( !m_configDummy )
338 m_configDummy = new wxMemoryConfig;
339 config = m_configDummy;
340
341 // FIXME: ideally, we should add keys from dummy config to a real one later,
e2478fde
VZ
342 // but it is a low-priority task because typical wxWin application
343 // either doesn't use wxConfig at all or creates wxConfig object in
344 // wxApp::OnInit(), before any real interaction with the user takes
345 // place...
346 }
347
5bbca8b0 348 return config;
e2478fde
VZ
349}
350
351const wxString& wxFontMapperBase::GetConfigPath()
352{
353 if ( !m_configRootPath )
354 {
355 // use the default
356 m_configRootPath = GetDefaultConfigPath();
357 }
358
359 return m_configRootPath;
360}
361
362// ----------------------------------------------------------------------------
363// config helpers
364// ----------------------------------------------------------------------------
365
366bool wxFontMapperBase::ChangePath(const wxString& pathNew, wxString *pathOld)
367{
368 wxConfigBase *config = GetConfig();
369 if ( !config )
a62848fd 370 return false;
e2478fde
VZ
371
372 *pathOld = config->GetPath();
373
374 wxString path = GetConfigPath();
375 if ( path.IsEmpty() || path.Last() != wxCONFIG_PATH_SEPARATOR )
376 {
377 path += wxCONFIG_PATH_SEPARATOR;
378 }
379
380 wxASSERT_MSG( !pathNew || (pathNew[0] != wxCONFIG_PATH_SEPARATOR),
381 wxT("should be a relative path") );
382
383 path += pathNew;
384
385 config->SetPath(path);
386
a62848fd 387 return true;
e2478fde
VZ
388}
389
390void wxFontMapperBase::RestorePath(const wxString& pathOld)
391{
392 GetConfig()->SetPath(pathOld);
393}
394
f1c75e0f
JS
395#endif
396
e2478fde
VZ
397// ----------------------------------------------------------------------------
398// charset/encoding correspondence
399// ----------------------------------------------------------------------------
400
401wxFontEncoding
402wxFontMapperBase::CharsetToEncoding(const wxString& charset,
403 bool WXUNUSED(interactive))
4dc55027
VZ
404{
405 int enc = NonInteractiveCharsetToEncoding(charset);
406 if ( enc == wxFONTENCODING_UNKNOWN )
407 {
408 // we should return wxFONTENCODING_SYSTEM from here for unknown
409 // encodings
410 enc = wxFONTENCODING_SYSTEM;
411 }
412
413 return (wxFontEncoding)enc;
414}
415
416int
417wxFontMapperBase::NonInteractiveCharsetToEncoding(const wxString& charset)
e2478fde
VZ
418{
419 wxFontEncoding encoding = wxFONTENCODING_SYSTEM;
420
421 // we're going to modify it, make a copy
422 wxString cs = charset;
423
4676948b 424#if wxUSE_CONFIG && wxUSE_FILECONFIG
e2478fde
VZ
425 // first try the user-defined settings
426 wxFontMapperPathChanger path(this, FONTMAPPER_CHARSET_PATH);
427 if ( path.IsOk() )
428 {
429 wxConfigBase *config = GetConfig();
430
431 // do we have an encoding for this charset?
432 long value = config->Read(charset, -1l);
433 if ( value != -1 )
434 {
435 if ( value == wxFONTENCODING_UNKNOWN )
436 {
437 // don't try to find it, in particular don't ask the user
4dc55027 438 return value;
e2478fde
VZ
439 }
440
441 if ( value >= 0 && value <= wxFONTENCODING_MAX )
442 {
443 encoding = (wxFontEncoding)value;
444 }
445 else
446 {
447 wxLogDebug(wxT("corrupted config data: invalid encoding %ld for charset '%s' ignored"),
448 value, charset.c_str());
449 }
450 }
451
452 if ( encoding == wxFONTENCODING_SYSTEM )
453 {
454 // may be we have an alias?
455 config->SetPath(FONTMAPPER_CHARSET_ALIAS_PATH);
456
457 wxString alias = config->Read(charset);
a62848fd 458 if ( !alias.IsEmpty() )
e2478fde
VZ
459 {
460 // yes, we do - use it instead
461 cs = alias;
462 }
463 }
464 }
465#endif // wxUSE_CONFIG
466
467 // if didn't find it there, try to recognize it ourselves
468 if ( encoding == wxFONTENCODING_SYSTEM )
469 {
470 // trim any spaces
471 cs.Trim(true);
472 cs.Trim(false);
473
474 // discard the optional quotes
475 if ( !cs.empty() )
476 {
477 if ( cs[0u] == _T('"') && cs.Last() == _T('"') )
478 {
479 cs = wxString(cs.c_str(), cs.length() - 1);
480 }
481 }
482
8b3eb85d 483 for ( size_t i = 0; i < WXSIZEOF(gs_encodingNames); ++i )
e2478fde 484 {
8b3eb85d
VZ
485 for ( const wxChar** encName = gs_encodingNames[i]; *encName; ++encName )
486 {
487 if ( cs.CmpNoCase(*encName) == 0 )
488 return gs_encodings[i];
489 }
15ad38c3 490 }
8b3eb85d
VZ
491
492 cs.MakeUpper();
493
494 if ( cs.Left(3) == wxT("ISO") )
e2478fde
VZ
495 {
496 // the dash is optional (or, to be exact, it is not, but
497 // several brokenmails "forget" it)
498 const wxChar *p = cs.c_str() + 3;
499 if ( *p == wxT('-') )
500 p++;
a62848fd 501
e2478fde
VZ
502 // printf( "iso %s\n", (const char*) cs.ToAscii() );
503
504 unsigned int value;
505 if ( wxSscanf(p, wxT("8859-%u"), &value) == 1 )
506 {
507 // printf( "value %d\n", (int)value );
a62848fd 508
e2478fde
VZ
509 // make it 0 based and check that it is strictly positive in
510 // the process (no such thing as iso8859-0 encoding)
511 if ( (value-- > 0) &&
512 (value < wxFONTENCODING_ISO8859_MAX -
513 wxFONTENCODING_ISO8859_1) )
514 {
515 // it's a valid ISO8859 encoding
516 value += wxFONTENCODING_ISO8859_1;
517 encoding = (wxFontEncoding)value;
518 }
519 }
520 }
521 else if ( cs.Left(4) == wxT("8859") )
522 {
523 const wxChar *p = cs.c_str();
a62848fd 524
e2478fde
VZ
525 unsigned int value;
526 if ( wxSscanf(p, wxT("8859-%u"), &value) == 1 )
527 {
528 // printf( "value %d\n", (int)value );
a62848fd 529
e2478fde
VZ
530 // make it 0 based and check that it is strictly positive in
531 // the process (no such thing as iso8859-0 encoding)
532 if ( (value-- > 0) &&
533 (value < wxFONTENCODING_ISO8859_MAX -
534 wxFONTENCODING_ISO8859_1) )
535 {
536 // it's a valid ISO8859 encoding
537 value += wxFONTENCODING_ISO8859_1;
538 encoding = (wxFontEncoding)value;
539 }
540 }
541 }
542 else // check for Windows charsets
543 {
544 size_t len;
545 if ( cs.Left(7) == wxT("WINDOWS") )
546 {
547 len = 7;
548 }
549 else if ( cs.Left(2) == wxT("CP") )
550 {
551 len = 2;
552 }
553 else // not a Windows encoding
554 {
555 len = 0;
556 }
557
558 if ( len )
559 {
560 const wxChar *p = cs.c_str() + len;
561 if ( *p == wxT('-') )
562 p++;
563
17a1ebd1 564 unsigned int value;
e2478fde
VZ
565 if ( wxSscanf(p, wxT("%u"), &value) == 1 )
566 {
567 if ( value >= 1250 )
568 {
569 value -= 1250;
570 if ( value < wxFONTENCODING_CP12_MAX -
571 wxFONTENCODING_CP1250 )
572 {
573 // a valid Windows code page
574 value += wxFONTENCODING_CP1250;
575 encoding = (wxFontEncoding)value;
576 }
577 }
578
579 switch ( value )
580 {
3c832d58
DS
581 case 874:
582 encoding = wxFONTENCODING_CP874;
583 break;
584
e2478fde
VZ
585 case 932:
586 encoding = wxFONTENCODING_CP932;
587 break;
588
589 case 936:
590 encoding = wxFONTENCODING_CP936;
591 break;
592
593 case 949:
594 encoding = wxFONTENCODING_CP949;
595 break;
596
597 case 950:
598 encoding = wxFONTENCODING_CP950;
599 break;
600 }
601 }
602 }
603 }
604 //else: unknown
605 }
606
607 return encoding;
608}
609
610/* static */
611size_t wxFontMapperBase::GetSupportedEncodingsCount()
612{
613 return WXSIZEOF(gs_encodings);
614}
615
616/* static */
617wxFontEncoding wxFontMapperBase::GetEncoding(size_t n)
618{
619 wxCHECK_MSG( n < WXSIZEOF(gs_encodings), wxFONTENCODING_SYSTEM,
620 _T("wxFontMapper::GetEncoding(): invalid index") );
621
622 return gs_encodings[n];
623}
624
625/* static */
626wxString wxFontMapperBase::GetEncodingDescription(wxFontEncoding encoding)
627{
628 if ( encoding == wxFONTENCODING_DEFAULT )
629 {
630 return _("Default encoding");
631 }
632
633 const size_t count = WXSIZEOF(gs_encodingDescs);
634
635 for ( size_t i = 0; i < count; i++ )
636 {
637 if ( gs_encodings[i] == encoding )
638 {
639 return wxGetTranslation(gs_encodingDescs[i]);
640 }
641 }
642
643 wxString str;
644 str.Printf(_("Unknown encoding (%d)"), encoding);
645
646 return str;
647}
648
649/* static */
650wxString wxFontMapperBase::GetEncodingName(wxFontEncoding encoding)
651{
652 if ( encoding == wxFONTENCODING_DEFAULT )
653 {
654 return _("default");
655 }
656
657 const size_t count = WXSIZEOF(gs_encodingNames);
658
659 for ( size_t i = 0; i < count; i++ )
660 {
661 if ( gs_encodings[i] == encoding )
662 {
8b3eb85d 663 return gs_encodingNames[i][0];
e2478fde
VZ
664 }
665 }
666
667 wxString str;
668 str.Printf(_("unknown-%d"), encoding);
669
670 return str;
671}
672
8b3eb85d
VZ
673/* static */
674const wxChar** wxFontMapperBase::GetAllEncodingNames(wxFontEncoding encoding)
675{
676 static const wxChar* dummy[] = { NULL };
677
678 for ( size_t i = 0; i < WXSIZEOF(gs_encodingNames); i++ )
679 {
680 if ( gs_encodings[i] == encoding )
681 {
682 return gs_encodingNames[i];
683 }
684 }
685
686 return dummy;
687}
688
910b9fc5
VZ
689/* static */
690wxFontEncoding wxFontMapperBase::GetEncodingFromName(const wxString& name)
691{
692 const size_t count = WXSIZEOF(gs_encodingNames);
693
694 for ( size_t i = 0; i < count; i++ )
695 {
8b3eb85d 696 for ( const wxChar** encName = gs_encodingNames[i]; *encName; ++encName )
910b9fc5 697 {
6ba5438d 698 if ( name.CmpNoCase(*encName) == 0 )
8b3eb85d 699 return gs_encodings[i];
910b9fc5
VZ
700 }
701 }
702
910b9fc5
VZ
703 return wxFONTENCODING_MAX;
704}
705
e2478fde
VZ
706#endif // wxUSE_FONTMAP
707