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