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"
46 // wxMemoryConfig uses wxFileConfig
47 #if wxUSE_CONFIG && wxUSE_FILECONFIG
48 #include "wx/config.h"
49 #include "wx/memconf.h"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // encodings supported by GetEncodingDescription
57 static wxFontEncoding gs_encodings
[] =
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
,
75 wxFONTENCODING_KOI8_U
,
81 wxFONTENCODING_CP1250
,
82 wxFONTENCODING_CP1251
,
83 wxFONTENCODING_CP1252
,
84 wxFONTENCODING_CP1253
,
85 wxFONTENCODING_CP1254
,
86 wxFONTENCODING_CP1255
,
87 wxFONTENCODING_CP1256
,
88 wxFONTENCODING_CP1257
,
93 wxFONTENCODING_UTF16BE
,
94 wxFONTENCODING_UTF16LE
,
96 wxFONTENCODING_UTF32BE
,
97 wxFONTENCODING_UTF32LE
,
98 wxFONTENCODING_EUC_JP
,
99 wxFONTENCODING_DEFAULT
,
101 wxFONTENCODING_SHIFT_JIS
,
102 wxFONTENCODING_GB2312
,
105 // the descriptions for them
106 static const wxChar
* gs_encodingDescs
[] =
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" ),
154 // and the internal names (these are not translated on purpose!)
155 static const wxChar
* gs_encodingNames
[WXSIZEOF(gs_encodingDescs
)][7] =
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
},
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
},
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
},
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
},
205 { wxT( "EUC-JP" ), wxT( "eucJP" ), wxT( "euc_jp" ), wxT( "IBM-eucJP" ), NULL
},
207 { wxT( "US-ASCII" ), wxT( "ASCII" ), wxT("ANSI_X3.4-1968"),
215 { wxT( "BIG5" ), NULL
},
216 { wxT( "shift-jis" ), wxT( "shift_jis" ), wxT( "sjis" ), NULL
},
217 { wxT( "GB2312" ), NULL
},
220 wxCOMPILE_TIME_ASSERT( WXSIZEOF(gs_encodingDescs
) == WXSIZEOF(gs_encodings
), EncodingsArraysNotInSync
);
221 wxCOMPILE_TIME_ASSERT( WXSIZEOF(gs_encodingNames
) == WXSIZEOF(gs_encodings
), EncodingsArraysNotInSync
);
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
227 // clean up the font mapper object
228 class wxFontMapperModule
: public wxModule
231 wxFontMapperModule() : wxModule() { }
232 virtual bool OnInit() { return true; }
233 virtual void OnExit() { delete (wxFontMapperBase
*)wxFontMapperBase::Set(NULL
); }
235 DECLARE_DYNAMIC_CLASS(wxFontMapperModule
)
238 IMPLEMENT_DYNAMIC_CLASS(wxFontMapperModule
, wxModule
)
241 // ============================================================================
242 // wxFontMapperBase implementation
243 // ============================================================================
245 wxFontMapper
*wxFontMapperBase::sm_instance
= NULL
;
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
251 wxFontMapperBase::wxFontMapperBase()
253 #if wxUSE_CONFIG && wxUSE_FILECONFIG
255 m_configIsDummy
= false;
256 #endif // wxUSE_CONFIG
259 wxFontMapperBase::~wxFontMapperBase()
261 #if wxUSE_CONFIG && wxUSE_FILECONFIG
262 if ( m_configIsDummy
)
264 #endif // wxUSE_CONFIG
267 bool wxFontMapperBase::IsWxFontMapper()
271 wxFontMapperBase
*wxFontMapperBase::Get()
275 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
278 sm_instance
= traits
->CreateFontMapper();
280 wxASSERT_MSG( sm_instance
,
281 _T("wxAppTraits::CreateFontMapper() failed") );
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
;
292 return (wxFontMapperBase
*)sm_instance
;
296 wxFontMapper
*wxFontMapperBase::Set(wxFontMapper
*mapper
)
298 wxFontMapper
*old
= sm_instance
;
299 sm_instance
= mapper
;
303 #if wxUSE_CONFIG && wxUSE_FILECONFIG
305 // ----------------------------------------------------------------------------
306 // config usage customisation
307 // ----------------------------------------------------------------------------
310 const wxChar
*wxFontMapperBase::GetDefaultConfigPath()
312 return FONTMAPPER_ROOT_PATH
;
315 void wxFontMapperBase::SetConfigPath(const wxString
& prefix
)
317 wxCHECK_RET( !prefix
.IsEmpty() && prefix
[0] == wxCONFIG_PATH_SEPARATOR
,
318 wxT("an absolute path should be given to wxFontMapper::SetConfigPath()") );
320 m_configRootPath
= prefix
;
323 // ----------------------------------------------------------------------------
324 // get config object and path for it
325 // ----------------------------------------------------------------------------
327 wxConfigBase
*wxFontMapperBase::GetConfig()
332 m_config
= wxConfig::Get(false /*don't create on demand*/ );
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())
350 if ( m_configIsDummy
&& wxConfig::Get(false) != NULL
)
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.
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
367 const wxString
& wxFontMapperBase::GetConfigPath()
369 if ( !m_configRootPath
)
372 m_configRootPath
= GetDefaultConfigPath();
375 return m_configRootPath
;
378 // ----------------------------------------------------------------------------
380 // ----------------------------------------------------------------------------
382 bool wxFontMapperBase::ChangePath(const wxString
& pathNew
, wxString
*pathOld
)
384 wxConfigBase
*config
= GetConfig();
388 *pathOld
= config
->GetPath();
390 wxString path
= GetConfigPath();
391 if ( path
.IsEmpty() || path
.Last() != wxCONFIG_PATH_SEPARATOR
)
393 path
+= wxCONFIG_PATH_SEPARATOR
;
396 wxASSERT_MSG( !pathNew
|| (pathNew
[0] != wxCONFIG_PATH_SEPARATOR
),
397 wxT("should be a relative path") );
401 config
->SetPath(path
);
406 void wxFontMapperBase::RestorePath(const wxString
& pathOld
)
408 GetConfig()->SetPath(pathOld
);
413 // ----------------------------------------------------------------------------
414 // charset/encoding correspondence
415 // ----------------------------------------------------------------------------
418 wxFontMapperBase::CharsetToEncoding(const wxString
& charset
,
419 bool WXUNUSED(interactive
))
421 int enc
= NonInteractiveCharsetToEncoding(charset
);
422 if ( enc
== wxFONTENCODING_UNKNOWN
)
424 // we should return wxFONTENCODING_SYSTEM from here for unknown
426 enc
= wxFONTENCODING_SYSTEM
;
429 return (wxFontEncoding
)enc
;
433 wxFontMapperBase::NonInteractiveCharsetToEncoding(const wxString
& charset
)
435 wxFontEncoding encoding
= wxFONTENCODING_SYSTEM
;
437 // we're going to modify it, make a copy
438 wxString cs
= charset
;
440 #if wxUSE_CONFIG && wxUSE_FILECONFIG
441 // first try the user-defined settings
442 wxFontMapperPathChanger
path(this, FONTMAPPER_CHARSET_PATH
);
445 wxConfigBase
*config
= GetConfig();
447 // do we have an encoding for this charset?
448 long value
= config
->Read(charset
, -1l);
451 if ( value
== wxFONTENCODING_UNKNOWN
)
453 // don't try to find it, in particular don't ask the user
457 if ( value
>= 0 && value
<= wxFONTENCODING_MAX
)
459 encoding
= (wxFontEncoding
)value
;
463 wxLogDebug(wxT("corrupted config data: invalid encoding %ld for charset '%s' ignored"),
464 value
, charset
.c_str());
468 if ( encoding
== wxFONTENCODING_SYSTEM
)
470 // may be we have an alias?
471 config
->SetPath(FONTMAPPER_CHARSET_ALIAS_PATH
);
473 wxString alias
= config
->Read(charset
);
474 if ( !alias
.IsEmpty() )
476 // yes, we do - use it instead
481 #endif // wxUSE_CONFIG
483 // if didn't find it there, try to recognize it ourselves
484 if ( encoding
== wxFONTENCODING_SYSTEM
)
490 // discard the optional quotes
493 if ( cs
[0u] == _T('"') && cs
.Last() == _T('"') )
495 cs
= wxString(cs
.c_str(), cs
.length() - 1);
499 for ( size_t i
= 0; i
< WXSIZEOF(gs_encodingNames
); ++i
)
501 for ( const wxChar
** encName
= gs_encodingNames
[i
]; *encName
; ++encName
)
503 if ( cs
.CmpNoCase(*encName
) == 0 )
504 return gs_encodings
[i
];
510 if ( cs
.Left(3) == wxT("ISO") )
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('-') )
518 // printf( "iso %s\n", (const char*) cs.ToAscii() );
521 if ( wxSscanf(p
, wxT("8859-%u"), &value
) == 1 )
523 // printf( "value %d\n", (int)value );
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
) )
531 // it's a valid ISO8859 encoding
532 value
+= wxFONTENCODING_ISO8859_1
;
533 encoding
= (wxFontEncoding
)value
;
537 else if ( cs
.Left(4) == wxT("8859") )
539 const wxChar
*p
= cs
.c_str();
542 if ( wxSscanf(p
, wxT("8859-%u"), &value
) == 1 )
544 // printf( "value %d\n", (int)value );
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
) )
552 // it's a valid ISO8859 encoding
553 value
+= wxFONTENCODING_ISO8859_1
;
554 encoding
= (wxFontEncoding
)value
;
558 else // check for Windows charsets
561 if ( cs
.Left(7) == wxT("WINDOWS") )
565 else if ( cs
.Left(2) == wxT("CP") )
569 else // not a Windows encoding
576 const wxChar
*p
= cs
.c_str() + len
;
577 if ( *p
== wxT('-') )
581 if ( wxSscanf(p
, wxT("%u"), &value
) == 1 )
586 if ( value
< wxFONTENCODING_CP12_MAX
-
587 wxFONTENCODING_CP1250
)
589 // a valid Windows code page
590 value
+= wxFONTENCODING_CP1250
;
591 encoding
= (wxFontEncoding
)value
;
598 encoding
= wxFONTENCODING_CP874
;
602 encoding
= wxFONTENCODING_CP932
;
606 encoding
= wxFONTENCODING_CP936
;
610 encoding
= wxFONTENCODING_CP949
;
614 encoding
= wxFONTENCODING_CP950
;
627 size_t wxFontMapperBase::GetSupportedEncodingsCount()
629 return WXSIZEOF(gs_encodings
);
633 wxFontEncoding
wxFontMapperBase::GetEncoding(size_t n
)
635 wxCHECK_MSG( n
< WXSIZEOF(gs_encodings
), wxFONTENCODING_SYSTEM
,
636 _T("wxFontMapper::GetEncoding(): invalid index") );
638 return gs_encodings
[n
];
642 wxString
wxFontMapperBase::GetEncodingDescription(wxFontEncoding encoding
)
644 if ( encoding
== wxFONTENCODING_DEFAULT
)
646 return _("Default encoding");
649 const size_t count
= WXSIZEOF(gs_encodingDescs
);
651 for ( size_t i
= 0; i
< count
; i
++ )
653 if ( gs_encodings
[i
] == encoding
)
655 return wxGetTranslation(gs_encodingDescs
[i
]);
660 str
.Printf(_("Unknown encoding (%d)"), encoding
);
666 wxString
wxFontMapperBase::GetEncodingName(wxFontEncoding encoding
)
668 if ( encoding
== wxFONTENCODING_DEFAULT
)
673 const size_t count
= WXSIZEOF(gs_encodingNames
);
675 for ( size_t i
= 0; i
< count
; i
++ )
677 if ( gs_encodings
[i
] == encoding
)
679 return gs_encodingNames
[i
][0];
684 str
.Printf(_("unknown-%d"), encoding
);
690 const wxChar
** wxFontMapperBase::GetAllEncodingNames(wxFontEncoding encoding
)
692 static const wxChar
* dummy
[] = { NULL
};
694 for ( size_t i
= 0; i
< WXSIZEOF(gs_encodingNames
); i
++ )
696 if ( gs_encodings
[i
] == encoding
)
698 return gs_encodingNames
[i
];
706 wxFontEncoding
wxFontMapperBase::GetEncodingFromName(const wxString
& name
)
708 const size_t count
= WXSIZEOF(gs_encodingNames
);
710 for ( size_t i
= 0; i
< count
; i
++ )
712 for ( const wxChar
** encName
= gs_encodingNames
[i
]; *encName
; ++encName
)
714 if ( name
.CmpNoCase(*encName
) == 0 )
715 return gs_encodings
[i
];
719 if ( name
== _("default") )
721 return wxFONTENCODING_DEFAULT
;
724 return wxFONTENCODING_MAX
;
727 #endif // wxUSE_FONTMAP