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