1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/fmapbase.cpp
3 // Purpose: wxFontMapperBase class implementation
4 // Author: Vadim Zeitlin
6 // Created: 21.06.2003 (extracted from common/fontmap.cpp)
8 // Copyright: (c) 1999-2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
35 #if defined(__WXMSW__)
36 #include "wx/msw/private.h" // includes windows.h for LOGFONT
37 #include "wx/msw/winundef.h"
40 #include "wx/fontmap.h"
41 #include "wx/fmappriv.h"
43 #include "wx/apptrait.h"
44 #include "wx/module.h"
47 #include "wx/config.h"
48 #include "wx/memconf.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // encodings supported by GetEncodingDescription
56 static wxFontEncoding gs_encodings
[] =
58 wxFONTENCODING_ISO8859_1
,
59 wxFONTENCODING_ISO8859_2
,
60 wxFONTENCODING_ISO8859_3
,
61 wxFONTENCODING_ISO8859_4
,
62 wxFONTENCODING_ISO8859_5
,
63 wxFONTENCODING_ISO8859_6
,
64 wxFONTENCODING_ISO8859_7
,
65 wxFONTENCODING_ISO8859_8
,
66 wxFONTENCODING_ISO8859_9
,
67 wxFONTENCODING_ISO8859_10
,
68 wxFONTENCODING_ISO8859_11
,
69 wxFONTENCODING_ISO8859_12
,
70 wxFONTENCODING_ISO8859_13
,
71 wxFONTENCODING_ISO8859_14
,
72 wxFONTENCODING_ISO8859_15
,
78 wxFONTENCODING_CP1250
,
79 wxFONTENCODING_CP1251
,
80 wxFONTENCODING_CP1252
,
81 wxFONTENCODING_CP1253
,
82 wxFONTENCODING_CP1254
,
83 wxFONTENCODING_CP1255
,
84 wxFONTENCODING_CP1256
,
85 wxFONTENCODING_CP1257
,
89 wxFONTENCODING_EUC_JP
,
92 // the descriptions for them
93 static const wxChar
* gs_encodingDescs
[] =
95 wxTRANSLATE( "Western European (ISO-8859-1)" ),
96 wxTRANSLATE( "Central European (ISO-8859-2)" ),
97 wxTRANSLATE( "Esperanto (ISO-8859-3)" ),
98 wxTRANSLATE( "Baltic (old) (ISO-8859-4)" ),
99 wxTRANSLATE( "Cyrillic (ISO-8859-5)" ),
100 wxTRANSLATE( "Arabic (ISO-8859-6)" ),
101 wxTRANSLATE( "Greek (ISO-8859-7)" ),
102 wxTRANSLATE( "Hebrew (ISO-8859-8)" ),
103 wxTRANSLATE( "Turkish (ISO-8859-9)" ),
104 wxTRANSLATE( "Nordic (ISO-8859-10)" ),
105 wxTRANSLATE( "Thai (ISO-8859-11)" ),
106 wxTRANSLATE( "Indian (ISO-8859-12)" ),
107 wxTRANSLATE( "Baltic (ISO-8859-13)" ),
108 wxTRANSLATE( "Celtic (ISO-8859-14)" ),
109 wxTRANSLATE( "Western European with Euro (ISO-8859-15)" ),
110 wxTRANSLATE( "KOI8-R" ),
111 wxTRANSLATE( "Windows Japanese (CP 932)" ),
112 wxTRANSLATE( "Windows Chinese Simplified (CP 936)" ),
113 wxTRANSLATE( "Windows Korean (CP 949)" ),
114 wxTRANSLATE( "Windows Chinese Traditional (CP 950)" ),
115 wxTRANSLATE( "Windows Central European (CP 1250)" ),
116 wxTRANSLATE( "Windows Cyrillic (CP 1251)" ),
117 wxTRANSLATE( "Windows Western European (CP 1252)" ),
118 wxTRANSLATE( "Windows Greek (CP 1253)" ),
119 wxTRANSLATE( "Windows Turkish (CP 1254)" ),
120 wxTRANSLATE( "Windows Hebrew (CP 1255)" ),
121 wxTRANSLATE( "Windows Arabic (CP 1256)" ),
122 wxTRANSLATE( "Windows Baltic (CP 1257)" ),
123 wxTRANSLATE( "Windows/DOS OEM (CP 437)" ),
124 wxTRANSLATE( "Unicode 7 bit (UTF-7)" ),
125 wxTRANSLATE( "Unicode 8 bit (UTF-8)" ),
126 wxTRANSLATE( "Extended Unix Codepage for Japanese (EUC-JP)" ),
129 // and the internal names (these are not translated on purpose!)
130 static const wxChar
* gs_encodingNames
[] =
141 wxT( "iso-8859-10" ),
142 wxT( "iso-8859-11" ),
143 wxT( "iso-8859-12" ),
144 wxT( "iso-8859-13" ),
145 wxT( "iso-8859-14" ),
146 wxT( "iso-8859-15" ),
148 wxT( "windows-932" ),
149 wxT( "windows-936" ),
150 wxT( "windows-949" ),
151 wxT( "windows-950" ),
152 wxT( "windows-1250" ),
153 wxT( "windows-1251" ),
154 wxT( "windows-1252" ),
155 wxT( "windows-1253" ),
156 wxT( "windows-1254" ),
157 wxT( "windows-1255" ),
158 wxT( "windows-1256" ),
159 wxT( "windows-1257" ),
160 wxT( "windows-437" ),
166 wxCOMPILE_TIME_ASSERT( WXSIZEOF(gs_encodingDescs
) == WXSIZEOF(gs_encodings
) &&
167 WXSIZEOF(gs_encodingNames
) == WXSIZEOF(gs_encodings
),
168 EncodingsArraysNotInSync
);
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 // clean up the font mapper object
175 class wxFontMapperModule
: public wxModule
178 wxFontMapperModule() : wxModule() { }
179 virtual bool OnInit() { return TRUE
; }
180 virtual void OnExit() { delete wxFontMapper::Set(NULL
); }
182 DECLARE_DYNAMIC_CLASS(wxFontMapperModule
)
185 IMPLEMENT_DYNAMIC_CLASS(wxFontMapperModule
, wxModule
)
188 // ============================================================================
189 // wxFontMapperBase implementation
190 // ============================================================================
192 wxFontMapper
*wxFontMapperBase::sm_instance
= NULL
;
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
198 wxFontMapperBase::wxFontMapperBase()
202 m_configIsDummy
= FALSE
;
203 #endif // wxUSE_CONFIG
206 wxFontMapperBase::~wxFontMapperBase()
209 if ( m_configIsDummy
)
211 #endif // wxUSE_CONFIG
215 wxFontMapper
*wxFontMapperBase::Get()
219 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
222 sm_instance
= traits
->CreateFontMapper();
224 wxASSERT_MSG( sm_instance
,
225 _T("wxAppTraits::CreateFontMapper() failed") );
230 // last resort: we must create something because the existing code
231 // relies on always having a valid font mapper object
232 sm_instance
= (wxFontMapper
*)new wxFontMapperBase
;
240 wxFontMapper
*wxFontMapperBase::Set(wxFontMapper
*mapper
)
242 wxFontMapper
*old
= sm_instance
;
243 sm_instance
= mapper
;
249 // ----------------------------------------------------------------------------
250 // config usage customisation
251 // ----------------------------------------------------------------------------
254 const wxChar
*wxFontMapperBase::GetDefaultConfigPath()
256 return FONTMAPPER_ROOT_PATH
;
259 void wxFontMapperBase::SetConfigPath(const wxString
& prefix
)
261 wxCHECK_RET( !prefix
.IsEmpty() && prefix
[0] == wxCONFIG_PATH_SEPARATOR
,
262 wxT("an absolute path should be given to wxFontMapper::SetConfigPath()") );
264 m_configRootPath
= prefix
;
267 // ----------------------------------------------------------------------------
268 // get config object and path for it
269 // ----------------------------------------------------------------------------
271 wxConfigBase
*wxFontMapperBase::GetConfig()
276 m_config
= wxConfig::Get(FALSE
/*don't create on demand*/ );
280 // we still want to have a config object because otherwise we would
281 // keep asking the user the same questions in the interactive mode,
282 // so create a dummy config which won't write to any files/registry
283 // but will allow us to remember the results of the questions at
284 // least during this run
285 m_config
= new wxMemoryConfig
;
286 m_configIsDummy
= TRUE
;
287 // VS: we can't call wxConfig::Set(m_config) here because that would
288 // disable automatic wxConfig instance creation if this code was
289 // called before wxApp::OnInit (this happens in wxGTK -- it sets
290 // default wxFont encoding in wxApp::Initialize())
294 if ( m_configIsDummy
&& wxConfig::Get(FALSE
) != NULL
)
296 // VS: in case we created dummy m_config (see above), we want to switch back
297 // to the real one as soon as one becomes available.
299 m_config
= wxConfig::Get(FALSE
);
300 m_configIsDummy
= FALSE
;
301 // FIXME: ideally, we should add keys from dummy config to the real one now,
302 // but it is a low-priority task because typical wxWin application
303 // either doesn't use wxConfig at all or creates wxConfig object in
304 // wxApp::OnInit(), before any real interaction with the user takes
311 const wxString
& wxFontMapperBase::GetConfigPath()
313 if ( !m_configRootPath
)
316 m_configRootPath
= GetDefaultConfigPath();
319 return m_configRootPath
;
322 // ----------------------------------------------------------------------------
324 // ----------------------------------------------------------------------------
326 bool wxFontMapperBase::ChangePath(const wxString
& pathNew
, wxString
*pathOld
)
328 wxConfigBase
*config
= GetConfig();
332 *pathOld
= config
->GetPath();
334 wxString path
= GetConfigPath();
335 if ( path
.IsEmpty() || path
.Last() != wxCONFIG_PATH_SEPARATOR
)
337 path
+= wxCONFIG_PATH_SEPARATOR
;
340 wxASSERT_MSG( !pathNew
|| (pathNew
[0] != wxCONFIG_PATH_SEPARATOR
),
341 wxT("should be a relative path") );
345 config
->SetPath(path
);
350 void wxFontMapperBase::RestorePath(const wxString
& pathOld
)
352 GetConfig()->SetPath(pathOld
);
355 // ----------------------------------------------------------------------------
356 // charset/encoding correspondence
357 // ----------------------------------------------------------------------------
360 wxFontMapperBase::CharsetToEncoding(const wxString
& charset
,
361 bool WXUNUSED(interactive
))
363 wxFontEncoding encoding
= wxFONTENCODING_SYSTEM
;
365 // we're going to modify it, make a copy
366 wxString cs
= charset
;
369 // first try the user-defined settings
370 wxFontMapperPathChanger
path(this, FONTMAPPER_CHARSET_PATH
);
373 wxConfigBase
*config
= GetConfig();
375 // do we have an encoding for this charset?
376 long value
= config
->Read(charset
, -1l);
379 if ( value
== wxFONTENCODING_UNKNOWN
)
381 // don't try to find it, in particular don't ask the user
382 return wxFONTENCODING_SYSTEM
;
385 if ( value
>= 0 && value
<= wxFONTENCODING_MAX
)
387 encoding
= (wxFontEncoding
)value
;
391 wxLogDebug(wxT("corrupted config data: invalid encoding %ld for charset '%s' ignored"),
392 value
, charset
.c_str());
396 if ( encoding
== wxFONTENCODING_SYSTEM
)
398 // may be we have an alias?
399 config
->SetPath(FONTMAPPER_CHARSET_ALIAS_PATH
);
401 wxString alias
= config
->Read(charset
);
404 // yes, we do - use it instead
409 #endif // wxUSE_CONFIG
411 // if didn't find it there, try to recognize it ourselves
412 if ( encoding
== wxFONTENCODING_SYSTEM
)
418 // discard the optional quotes
421 if ( cs
[0u] == _T('"') && cs
.Last() == _T('"') )
423 cs
= wxString(cs
.c_str(), cs
.length() - 1);
429 if ( cs
.empty() || cs
== _T("US-ASCII") )
431 encoding
= wxFONTENCODING_DEFAULT
;
433 else if ( cs
== wxT("UTF-7") )
435 encoding
= wxFONTENCODING_UTF7
;
437 else if ( cs
== wxT("UTF-8") )
439 encoding
= wxFONTENCODING_UTF8
;
441 else if ( cs
== wxT("GB2312") )
443 encoding
= wxFONTENCODING_GB2312
;
445 else if ( cs
== wxT("BIG5") )
447 encoding
= wxFONTENCODING_BIG5
;
449 else if ( cs
== wxT("SJIS") ||
450 cs
== wxT("SHIFT_JIS") ||
451 cs
== wxT("SHIFT-JIS") )
453 encoding
= wxFONTENCODING_SHIFT_JIS
;
455 else if ( cs
== wxT("EUC-JP") ||
456 cs
== wxT("EUC_JP") )
458 encoding
= wxFONTENCODING_EUC_JP
;
460 else if ( cs
== wxT("KOI8-R") ||
461 cs
== wxT("KOI8-U") ||
462 cs
== wxT("KOI8-RU") )
464 // although koi8-ru is not strictly speaking the same as koi8-r,
465 // they are similar enough to make mapping it to koi8 better than
466 // not reckognizing it at all
467 encoding
= wxFONTENCODING_KOI8
;
469 else if ( cs
.Left(3) == wxT("ISO") )
471 // the dash is optional (or, to be exact, it is not, but
472 // several brokenmails "forget" it)
473 const wxChar
*p
= cs
.c_str() + 3;
474 if ( *p
== wxT('-') )
477 // printf( "iso %s\n", (const char*) cs.ToAscii() );
480 if ( wxSscanf(p
, wxT("8859-%u"), &value
) == 1 )
482 // printf( "value %d\n", (int)value );
484 // make it 0 based and check that it is strictly positive in
485 // the process (no such thing as iso8859-0 encoding)
486 if ( (value
-- > 0) &&
487 (value
< wxFONTENCODING_ISO8859_MAX
-
488 wxFONTENCODING_ISO8859_1
) )
490 // it's a valid ISO8859 encoding
491 value
+= wxFONTENCODING_ISO8859_1
;
492 encoding
= (wxFontEncoding
)value
;
496 else if ( cs
.Left(4) == wxT("8859") )
498 const wxChar
*p
= cs
.c_str();
501 if ( wxSscanf(p
, wxT("8859-%u"), &value
) == 1 )
503 // printf( "value %d\n", (int)value );
505 // make it 0 based and check that it is strictly positive in
506 // the process (no such thing as iso8859-0 encoding)
507 if ( (value
-- > 0) &&
508 (value
< wxFONTENCODING_ISO8859_MAX
-
509 wxFONTENCODING_ISO8859_1
) )
511 // it's a valid ISO8859 encoding
512 value
+= wxFONTENCODING_ISO8859_1
;
513 encoding
= (wxFontEncoding
)value
;
517 else // check for Windows charsets
520 if ( cs
.Left(7) == wxT("WINDOWS") )
524 else if ( cs
.Left(2) == wxT("CP") )
528 else // not a Windows encoding
535 const wxChar
*p
= cs
.c_str() + len
;
536 if ( *p
== wxT('-') )
540 if ( wxSscanf(p
, wxT("%u"), &value
) == 1 )
545 if ( value
< wxFONTENCODING_CP12_MAX
-
546 wxFONTENCODING_CP1250
)
548 // a valid Windows code page
549 value
+= wxFONTENCODING_CP1250
;
550 encoding
= (wxFontEncoding
)value
;
557 encoding
= wxFONTENCODING_CP932
;
561 encoding
= wxFONTENCODING_CP936
;
565 encoding
= wxFONTENCODING_CP949
;
569 encoding
= wxFONTENCODING_CP950
;
582 size_t wxFontMapperBase::GetSupportedEncodingsCount()
584 return WXSIZEOF(gs_encodings
);
588 wxFontEncoding
wxFontMapperBase::GetEncoding(size_t n
)
590 wxCHECK_MSG( n
< WXSIZEOF(gs_encodings
), wxFONTENCODING_SYSTEM
,
591 _T("wxFontMapper::GetEncoding(): invalid index") );
593 return gs_encodings
[n
];
597 wxString
wxFontMapperBase::GetEncodingDescription(wxFontEncoding encoding
)
599 if ( encoding
== wxFONTENCODING_DEFAULT
)
601 return _("Default encoding");
604 const size_t count
= WXSIZEOF(gs_encodingDescs
);
606 for ( size_t i
= 0; i
< count
; i
++ )
608 if ( gs_encodings
[i
] == encoding
)
610 return wxGetTranslation(gs_encodingDescs
[i
]);
615 str
.Printf(_("Unknown encoding (%d)"), encoding
);
621 wxString
wxFontMapperBase::GetEncodingName(wxFontEncoding encoding
)
623 if ( encoding
== wxFONTENCODING_DEFAULT
)
628 const size_t count
= WXSIZEOF(gs_encodingNames
);
630 for ( size_t i
= 0; i
< count
; i
++ )
632 if ( gs_encodings
[i
] == encoding
)
634 return gs_encodingNames
[i
];
639 str
.Printf(_("unknown-%d"), encoding
);
644 #endif // wxUSE_CONFIG
646 #endif // wxUSE_FONTMAP