1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
33 #include "wx/module.h"
34 #include "wx/wxcrtvararg.h"
37 #if defined(__WXMSW__)
38 #include "wx/msw/private.h" // includes windows.h for LOGFONT
39 #include "wx/msw/winundef.h"
42 #include "wx/fontmap.h"
43 #include "wx/fmappriv.h"
45 #include "wx/apptrait.h"
47 // wxMemoryConfig uses wxFileConfig
48 #if wxUSE_CONFIG && wxUSE_FILECONFIG
49 #include "wx/config.h"
50 #include "wx/memconf.h"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // encodings supported by GetEncodingDescription
58 static const wxFontEncoding gs_encodings
[] =
60 wxFONTENCODING_ISO8859_1
,
61 wxFONTENCODING_ISO8859_2
,
62 wxFONTENCODING_ISO8859_3
,
63 wxFONTENCODING_ISO8859_4
,
64 wxFONTENCODING_ISO8859_5
,
65 wxFONTENCODING_ISO8859_6
,
66 wxFONTENCODING_ISO8859_7
,
67 wxFONTENCODING_ISO8859_8
,
68 wxFONTENCODING_ISO8859_9
,
69 wxFONTENCODING_ISO8859_10
,
70 wxFONTENCODING_ISO8859_11
,
71 wxFONTENCODING_ISO8859_12
,
72 wxFONTENCODING_ISO8859_13
,
73 wxFONTENCODING_ISO8859_14
,
74 wxFONTENCODING_ISO8859_15
,
76 wxFONTENCODING_KOI8_U
,
83 wxFONTENCODING_CP1250
,
84 wxFONTENCODING_CP1251
,
85 wxFONTENCODING_CP1252
,
86 wxFONTENCODING_CP1253
,
87 wxFONTENCODING_CP1254
,
88 wxFONTENCODING_CP1255
,
89 wxFONTENCODING_CP1256
,
90 wxFONTENCODING_CP1257
,
94 wxFONTENCODING_UTF16BE
,
95 wxFONTENCODING_UTF16LE
,
96 wxFONTENCODING_UTF32BE
,
97 wxFONTENCODING_UTF32LE
,
98 wxFONTENCODING_EUC_JP
,
99 wxFONTENCODING_DEFAULT
,
100 wxFONTENCODING_ISO2022_JP
,
102 wxFONTENCODING_MACROMAN
,
103 wxFONTENCODING_MACJAPANESE
,
104 wxFONTENCODING_MACCHINESETRAD
,
105 wxFONTENCODING_MACKOREAN
,
106 wxFONTENCODING_MACARABIC
,
107 wxFONTENCODING_MACHEBREW
,
108 wxFONTENCODING_MACGREEK
,
109 wxFONTENCODING_MACCYRILLIC
,
110 wxFONTENCODING_MACDEVANAGARI
,
111 wxFONTENCODING_MACGURMUKHI
,
112 wxFONTENCODING_MACGUJARATI
,
113 wxFONTENCODING_MACORIYA
,
114 wxFONTENCODING_MACBENGALI
,
115 wxFONTENCODING_MACTAMIL
,
116 wxFONTENCODING_MACTELUGU
,
117 wxFONTENCODING_MACKANNADA
,
118 wxFONTENCODING_MACMALAJALAM
,
119 wxFONTENCODING_MACSINHALESE
,
120 wxFONTENCODING_MACBURMESE
,
121 wxFONTENCODING_MACKHMER
,
122 wxFONTENCODING_MACTHAI
,
123 wxFONTENCODING_MACLAOTIAN
,
124 wxFONTENCODING_MACGEORGIAN
,
125 wxFONTENCODING_MACARMENIAN
,
126 wxFONTENCODING_MACCHINESESIMP
,
127 wxFONTENCODING_MACTIBETAN
,
128 wxFONTENCODING_MACMONGOLIAN
,
129 wxFONTENCODING_MACETHIOPIC
,
130 wxFONTENCODING_MACCENTRALEUR
,
131 wxFONTENCODING_MACVIATNAMESE
,
132 wxFONTENCODING_MACARABICEXT
,
133 wxFONTENCODING_MACSYMBOL
,
134 wxFONTENCODING_MACDINGBATS
,
135 wxFONTENCODING_MACTURKISH
,
136 wxFONTENCODING_MACCROATIAN
,
137 wxFONTENCODING_MACICELANDIC
,
138 wxFONTENCODING_MACROMANIAN
,
139 wxFONTENCODING_MACCELTIC
,
140 wxFONTENCODING_MACGAELIC
,
141 wxFONTENCODING_MACKEYBOARD
144 // the descriptions for them
145 static const char* const gs_encodingDescs
[] =
147 wxTRANSLATE( "Western European (ISO-8859-1)" ),
148 wxTRANSLATE( "Central European (ISO-8859-2)" ),
149 wxTRANSLATE( "Esperanto (ISO-8859-3)" ),
150 wxTRANSLATE( "Baltic (old) (ISO-8859-4)" ),
151 wxTRANSLATE( "Cyrillic (ISO-8859-5)" ),
152 wxTRANSLATE( "Arabic (ISO-8859-6)" ),
153 wxTRANSLATE( "Greek (ISO-8859-7)" ),
154 wxTRANSLATE( "Hebrew (ISO-8859-8)" ),
155 wxTRANSLATE( "Turkish (ISO-8859-9)" ),
156 wxTRANSLATE( "Nordic (ISO-8859-10)" ),
157 wxTRANSLATE( "Thai (ISO-8859-11)" ),
158 wxTRANSLATE( "Indian (ISO-8859-12)" ),
159 wxTRANSLATE( "Baltic (ISO-8859-13)" ),
160 wxTRANSLATE( "Celtic (ISO-8859-14)" ),
161 wxTRANSLATE( "Western European with Euro (ISO-8859-15)" ),
162 wxTRANSLATE( "KOI8-R" ),
163 wxTRANSLATE( "KOI8-U" ),
164 wxTRANSLATE( "Windows/DOS OEM Cyrillic (CP 866)" ),
165 wxTRANSLATE( "Windows Thai (CP 874)" ),
166 wxTRANSLATE( "Windows Japanese (CP 932) or Shift-JIS" ),
167 wxTRANSLATE( "Windows Chinese Simplified (CP 936) or GB-2312" ),
168 wxTRANSLATE( "Windows Korean (CP 949)" ),
169 wxTRANSLATE( "Windows Chinese Traditional (CP 950) or Big-5" ),
170 wxTRANSLATE( "Windows Central European (CP 1250)" ),
171 wxTRANSLATE( "Windows Cyrillic (CP 1251)" ),
172 wxTRANSLATE( "Windows Western European (CP 1252)" ),
173 wxTRANSLATE( "Windows Greek (CP 1253)" ),
174 wxTRANSLATE( "Windows Turkish (CP 1254)" ),
175 wxTRANSLATE( "Windows Hebrew (CP 1255)" ),
176 wxTRANSLATE( "Windows Arabic (CP 1256)" ),
177 wxTRANSLATE( "Windows Baltic (CP 1257)" ),
178 wxTRANSLATE( "Windows/DOS OEM (CP 437)" ),
179 wxTRANSLATE( "Unicode 7 bit (UTF-7)" ),
180 wxTRANSLATE( "Unicode 8 bit (UTF-8)" ),
181 #ifdef WORDS_BIGENDIAN
182 wxTRANSLATE( "Unicode 16 bit (UTF-16)" ),
183 wxTRANSLATE( "Unicode 16 bit Little Endian (UTF-16LE)" ),
184 wxTRANSLATE( "Unicode 32 bit (UTF-32)" ),
185 wxTRANSLATE( "Unicode 32 bit Little Endian (UTF-32LE)" ),
186 #else // WORDS_BIGENDIAN
187 wxTRANSLATE( "Unicode 16 bit Big Endian (UTF-16BE)" ),
188 wxTRANSLATE( "Unicode 16 bit (UTF-16)" ),
189 wxTRANSLATE( "Unicode 32 bit Big Endian (UTF-32BE)" ),
190 wxTRANSLATE( "Unicode 32 bit (UTF-32)" ),
191 #endif // WORDS_BIGENDIAN
192 wxTRANSLATE( "Extended Unix Codepage for Japanese (EUC-JP)" ),
193 wxTRANSLATE( "US-ASCII" ),
194 wxTRANSLATE( "ISO-2022-JP" ),
196 wxTRANSLATE( "MacRoman" ),
197 wxTRANSLATE( "MacJapanese" ),
198 wxTRANSLATE( "MacChineseTrad" ),
199 wxTRANSLATE( "MacKorean" ),
200 wxTRANSLATE( "MacArabic" ),
201 wxTRANSLATE( "MacHebrew" ),
202 wxTRANSLATE( "MacGreek" ),
203 wxTRANSLATE( "MacCyrillic" ),
204 wxTRANSLATE( "MacDevanagari" ),
205 wxTRANSLATE( "MacGurmukhi" ),
206 wxTRANSLATE( "MacGujarati" ),
207 wxTRANSLATE( "MacOriya" ),
208 wxTRANSLATE( "MacBengali" ),
209 wxTRANSLATE( "MacTamil" ),
210 wxTRANSLATE( "MacTelugu" ),
211 wxTRANSLATE( "MacKannada" ),
212 wxTRANSLATE( "MacMalayalam" ),
213 wxTRANSLATE( "MacSinhalese" ),
214 wxTRANSLATE( "MacBurmese" ),
215 wxTRANSLATE( "MacKhmer" ),
216 wxTRANSLATE( "MacThai" ),
217 wxTRANSLATE( "MacLaotian" ),
218 wxTRANSLATE( "MacGeorgian" ),
219 wxTRANSLATE( "MacArmenian" ),
220 wxTRANSLATE( "MacChineseSimp" ),
221 wxTRANSLATE( "MacTibetan" ),
222 wxTRANSLATE( "MacMongolian" ),
223 wxTRANSLATE( "MacEthiopic" ),
224 wxTRANSLATE( "MacCentralEurRoman" ),
225 wxTRANSLATE( "MacVietnamese" ),
226 wxTRANSLATE( "MacExtArabic" ),
227 wxTRANSLATE( "MacSymbol" ),
228 wxTRANSLATE( "MacDingbats" ),
229 wxTRANSLATE( "MacTurkish" ),
230 wxTRANSLATE( "MacCroatian" ),
231 wxTRANSLATE( "MacIcelandic" ),
232 wxTRANSLATE( "MacRomanian" ),
233 wxTRANSLATE( "MacCeltic" ),
234 wxTRANSLATE( "MacGaelic" ),
235 wxTRANSLATE( "MacKeyboardGlyphs" )
238 // and the internal names (these are not translated on purpose!)
239 static const wxChar
* const gs_encodingNames
[][9] =
241 // names from the columns correspond to these OS:
242 // Linux Solaris and IRIX HP-UX AIX
243 { wxT("ISO-8859-1"), wxT("ISO8859-1"), wxT("iso88591"), wxT("8859-1"), wxT("iso_8859_1"), NULL
},
244 { wxT("ISO-8859-2"), wxT("ISO8859-2"), wxT("iso88592"), wxT("8859-2"), NULL
},
245 { wxT("ISO-8859-3"), wxT("ISO8859-3"), wxT("iso88593"), wxT("8859-3"), NULL
},
246 { wxT("ISO-8859-4"), wxT("ISO8859-4"), wxT("iso88594"), wxT("8859-4"), NULL
},
247 { wxT("ISO-8859-5"), wxT("ISO8859-5"), wxT("iso88595"), wxT("8859-5"), NULL
},
248 { wxT("ISO-8859-6"), wxT("ISO8859-6"), wxT("iso88596"), wxT("8859-6"), NULL
},
249 { wxT("ISO-8859-7"), wxT("ISO8859-7"), wxT("iso88597"), wxT("8859-7"), NULL
},
250 { wxT("ISO-8859-8"), wxT("ISO8859-8"), wxT("iso88598"), wxT("8859-8"), NULL
},
251 { wxT("ISO-8859-9"), wxT("ISO8859-9"), wxT("iso88599"), wxT("8859-9"), NULL
},
252 { wxT("ISO-8859-10"), wxT("ISO8859-10"), wxT("iso885910"), wxT("8859-10"), NULL
},
253 { wxT("ISO-8859-11"), wxT("ISO8859-11"), wxT("iso885911"), wxT("8859-11"), NULL
},
254 { wxT("ISO-8859-12"), wxT("ISO8859-12"), wxT("iso885912"), wxT("8859-12"), NULL
},
255 { wxT("ISO-8859-13"), wxT("ISO8859-13"), wxT("iso885913"), wxT("8859-13"), NULL
},
256 { wxT("ISO-8859-14"), wxT("ISO8859-14"), wxT("iso885914"), wxT("8859-14"), NULL
},
257 { wxT("ISO-8859-15"), wxT("ISO8859-15"), wxT("iso885915"), wxT("8859-15"), NULL
},
259 // although koi8-ru is not strictly speaking the same as koi8-r,
260 // they are similar enough to make mapping it to koi8 better than
261 // not recognizing it at all
262 { wxT( "KOI8-R" ), wxT( "KOI8-RU" ), NULL
},
263 { wxT( "KOI8-U" ), NULL
},
265 { wxT( "WINDOWS-866" ), wxT( "CP866" ), NULL
},
267 { wxT( "WINDOWS-874" ), wxT( "CP874" ), wxT( "MS874" ), wxT( "IBM-874" ), NULL
},
268 { wxT( "WINDOWS-932" ), wxT( "CP932" ), wxT( "MS932" ), wxT( "IBM-932" ), wxT( "SJIS" ), wxT( "SHIFT-JIS" ), wxT( "SHIFT_JIS" ), NULL
},
269 { wxT( "WINDOWS-936" ), wxT( "CP936" ), wxT( "MS936" ), wxT( "IBM-936" ), wxT( "GB2312" ), NULL
},
270 { wxT( "WINDOWS-949" ), wxT( "CP949" ), wxT( "MS949" ), wxT( "IBM-949" ), wxT( "EUC-KR" ), wxT( "eucKR" ), wxT( "euc_kr" ), NULL
},
271 { wxT( "WINDOWS-950" ), wxT( "CP950" ), wxT( "MS950" ), wxT( "IBM-950" ), wxT( "BIG5" ), wxT( "BIG-5" ), wxT( "BIG-FIVE" ), NULL
},
272 { wxT( "WINDOWS-1250" ),wxT( "CP1250" ),wxT( "MS1250" ),wxT( "IBM-1250" ),NULL
},
273 { wxT( "WINDOWS-1251" ),wxT( "CP1251" ),wxT( "MS1251" ),wxT( "IBM-1251" ),NULL
},
274 { wxT( "WINDOWS-1252" ),wxT( "CP1252" ),wxT( "MS1252" ),wxT( "IBM-1252" ),NULL
},
275 { wxT( "WINDOWS-1253" ),wxT( "CP1253" ),wxT( "MS1253" ),wxT( "IBM-1253" ),NULL
},
276 { wxT( "WINDOWS-1254" ),wxT( "CP1254" ),wxT( "MS1254" ),wxT( "IBM-1254" ),NULL
},
277 { wxT( "WINDOWS-1255" ),wxT( "CP1255" ),wxT( "MS1255" ),wxT( "IBM-1255" ),NULL
},
278 { wxT( "WINDOWS-1256" ),wxT( "CP1256" ),wxT( "MS1256" ),wxT( "IBM-1256" ),NULL
},
279 { wxT( "WINDOWS-1257" ),wxT( "CP1257" ),wxT( "MS1257" ),wxT( "IBM-1257" ),NULL
},
280 { wxT( "WINDOWS-437" ), wxT( "CP437" ), wxT( "MS437" ), wxT( "IBM-437" ), NULL
},
282 { wxT( "UTF-7" ), wxT("UTF7"), NULL
},
283 { wxT( "UTF-8" ), wxT("UTF8"), NULL
},
284 #ifdef WORDS_BIGENDIAN
285 { wxT( "UTF-16BE" ), wxT("UTF16BE"), wxT("UCS-2BE"), wxT("UCS2BE"), wxT("UTF-16"), wxT("UTF16"), wxT("UCS-2"), wxT("UCS2"), NULL
},
286 { wxT( "UTF-16LE" ), wxT("UTF16LE"), wxT("UCS-2LE"), wxT("UCS2LE"), NULL
},
287 { wxT( "UTF-32BE" ), wxT("UTF32BE"), wxT("UCS-4BE" ), wxT("UTF-32"), wxT("UTF32"), wxT("UCS-4"), wxT("UCS4"), NULL
},
288 { wxT( "UTF-32LE" ), wxT("UTF32LE"), wxT("UCS-4LE"), wxT("UCS4LE"), NULL
},
289 #else // WORDS_BIGENDIAN
290 { wxT("UTF-16BE"), wxT("UTF16BE"), wxT("UCS-2BE"), wxT("UCS2BE"), NULL
},
291 { wxT("UTF-16LE"), wxT("UTF16LE"), wxT("UCS-2LE"), wxT("UTF-16"), wxT("UTF16"), wxT("UCS-2"), wxT("UCS2"), NULL
},
292 { wxT("UTF-32BE"), wxT("UTF32BE"), wxT("UCS-4BE"), wxT("UCS4BE"), NULL
},
293 { wxT("UTF-32LE"), wxT("UTF32LE"), wxT("UCS-4LE"), wxT("UCS4LE"), wxT("UTF-32"), wxT("UTF32"), wxT("UCS-4"), wxT("UCS4"), NULL
},
294 #endif // WORDS_BIGENDIAN
296 { wxT( "EUC-JP" ), wxT( "eucJP" ), wxT( "euc_jp" ), wxT( "IBM-eucJP" ), NULL
},
298 // 646 is for Solaris, roman8 -- for HP-UX
299 { wxT( "US-ASCII" ), wxT( "ASCII" ), wxT("C"), wxT("POSIX"), wxT("ANSI_X3.4-1968"),
300 wxT("646"), wxT("roman8"), wxT( "" ), NULL
},
302 { wxT( "ISO-2022-JP" ), NULL
},
305 { wxT( "MacRoman" ), NULL
},
306 { wxT( "MacJapanese" ), NULL
},
307 { wxT( "MacChineseTrad" ), NULL
},
308 { wxT( "MacKorean" ), NULL
},
309 { wxT( "MacArabic" ), NULL
},
310 { wxT( "MacHebrew" ), NULL
},
311 { wxT( "MacGreek" ), NULL
},
312 { wxT( "MacCyrillic" ), NULL
},
313 { wxT( "MacDevanagari" ), NULL
},
314 { wxT( "MacGurmukhi" ), NULL
},
315 { wxT( "MacGujarati" ), NULL
},
316 { wxT( "MacOriya" ), NULL
},
317 { wxT( "MacBengali" ), NULL
},
318 { wxT( "MacTamil" ), NULL
},
319 { wxT( "MacTelugu" ), NULL
},
320 { wxT( "MacKannada" ), NULL
},
321 { wxT( "MacMalayalam" ), NULL
},
322 { wxT( "MacSinhalese" ), NULL
},
323 { wxT( "MacBurmese" ), NULL
},
324 { wxT( "MacKhmer" ), NULL
},
325 { wxT( "MacThai" ), NULL
},
326 { wxT( "MacLaotian" ), NULL
},
327 { wxT( "MacGeorgian" ), NULL
},
328 { wxT( "MacArmenian" ), NULL
},
329 { wxT( "MacChineseSimp" ), NULL
},
330 { wxT( "MacTibetan" ), NULL
},
331 { wxT( "MacMongolian" ), NULL
},
332 { wxT( "MacEthiopic" ), NULL
},
333 { wxT( "MacCentralEurRoman" ), NULL
},
334 { wxT( "MacVietnamese" ), NULL
},
335 { wxT( "MacExtArabic" ), NULL
},
336 { wxT( "MacSymbol" ), NULL
},
337 { wxT( "MacDingbats" ), NULL
},
338 { wxT( "MacTurkish" ), NULL
},
339 { wxT( "MacCroatian" ), NULL
},
340 { wxT( "MacIcelandic" ), NULL
},
341 { wxT( "MacRomanian" ), NULL
},
342 { wxT( "MacCeltic" ), NULL
},
343 { wxT( "MacGaelic" ), NULL
},
344 { wxT( "MacKeyboardGlyphs" ), NULL
}
347 wxCOMPILE_TIME_ASSERT( WXSIZEOF(gs_encodingDescs
) == WXSIZEOF(gs_encodings
), EncodingsArraysNotInSync
);
348 wxCOMPILE_TIME_ASSERT( WXSIZEOF(gs_encodingNames
) == WXSIZEOF(gs_encodings
), EncodingsArraysNotInSync
);
350 // ----------------------------------------------------------------------------
352 // ----------------------------------------------------------------------------
354 // clean up the font mapper object
355 class wxFontMapperModule
: public wxModule
358 wxFontMapperModule() : wxModule() { }
360 virtual bool OnInit()
362 // a dummy wxFontMapperBase object could have been created during the
363 // program startup before wxApp was created, we have to delete it to
364 // allow creating the real font mapper next time it is needed now that
365 // we can create it (when the modules are initialized, wxApp object
367 wxFontMapperBase
*fm
= wxFontMapperBase::Get();
368 if ( fm
&& fm
->IsDummy() )
369 wxFontMapperBase::Reset();
374 virtual void OnExit()
376 wxFontMapperBase::Reset();
379 DECLARE_DYNAMIC_CLASS(wxFontMapperModule
)
382 IMPLEMENT_DYNAMIC_CLASS(wxFontMapperModule
, wxModule
)
385 // ============================================================================
386 // wxFontMapperBase implementation
387 // ============================================================================
389 wxFontMapper
*wxFontMapperBase::sm_instance
= NULL
;
391 // ----------------------------------------------------------------------------
393 // ----------------------------------------------------------------------------
395 wxFontMapperBase::wxFontMapperBase()
397 #if wxUSE_CONFIG && wxUSE_FILECONFIG
398 m_configDummy
= NULL
;
399 #endif // wxUSE_CONFIG
402 wxFontMapperBase::~wxFontMapperBase()
404 #if wxUSE_CONFIG && wxUSE_FILECONFIG
406 delete m_configDummy
;
407 #endif // wxUSE_CONFIG
411 wxFontMapperBase
*wxFontMapperBase::Get()
415 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
418 sm_instance
= traits
->CreateFontMapper();
420 wxASSERT_MSG( sm_instance
,
421 wxT("wxAppTraits::CreateFontMapper() failed") );
426 // last resort: we must create something because the existing code
427 // relies on always having a valid font mapper object
428 sm_instance
= (wxFontMapper
*)new wxFontMapperBase
;
432 return (wxFontMapperBase
*)sm_instance
;
436 wxFontMapper
*wxFontMapperBase::Set(wxFontMapper
*mapper
)
438 wxFontMapper
*old
= sm_instance
;
439 sm_instance
= mapper
;
444 void wxFontMapperBase::Reset()
448 // we need a cast as wxFontMapper is not fully declared here and so the
449 // compiler can't know that it derives from wxFontMapperBase (but
450 // run-time behaviour will be correct because the dtor is virtual)
451 delete (wxFontMapperBase
*)sm_instance
;
456 #if wxUSE_CONFIG && wxUSE_FILECONFIG
458 // ----------------------------------------------------------------------------
459 // config usage customisation
460 // ----------------------------------------------------------------------------
463 static wxString
gs_defaultConfigPath(FONTMAPPER_ROOT_PATH
);
466 const wxString
& wxFontMapperBase::GetDefaultConfigPath()
468 // NB: we return const wxString& and not wxString for compatibility
469 // with 2.8 that returned const wxChar*
470 return gs_defaultConfigPath
;
473 void wxFontMapperBase::SetConfigPath(const wxString
& prefix
)
475 wxCHECK_RET( !prefix
.empty() && prefix
[0] == wxCONFIG_PATH_SEPARATOR
,
476 wxT("an absolute path should be given to wxFontMapper::SetConfigPath()") );
478 m_configRootPath
= prefix
;
481 // ----------------------------------------------------------------------------
482 // get config object and path for it
483 // ----------------------------------------------------------------------------
485 wxConfigBase
*wxFontMapperBase::GetConfig()
487 wxConfigBase
*config
= wxConfig::Get(false);
489 // If there is no global configuration, use an internal memory configuration
492 if ( !m_configDummy
)
493 m_configDummy
= new wxMemoryConfig
;
494 config
= m_configDummy
;
496 // FIXME: ideally, we should add keys from dummy config to a real one later,
497 // but it is a low-priority task because typical wxWin application
498 // either doesn't use wxConfig at all or creates wxConfig object in
499 // wxApp::OnInit(), before any real interaction with the user takes
506 const wxString
& wxFontMapperBase::GetConfigPath()
508 if ( !m_configRootPath
)
511 m_configRootPath
= GetDefaultConfigPath();
514 return m_configRootPath
;
517 // ----------------------------------------------------------------------------
519 // ----------------------------------------------------------------------------
521 bool wxFontMapperBase::ChangePath(const wxString
& pathNew
, wxString
*pathOld
)
523 wxConfigBase
*config
= GetConfig();
527 *pathOld
= config
->GetPath();
529 wxString path
= GetConfigPath();
530 if ( path
.empty() || path
.Last() != wxCONFIG_PATH_SEPARATOR
)
532 path
+= wxCONFIG_PATH_SEPARATOR
;
535 wxASSERT_MSG( !pathNew
|| (pathNew
[0] != wxCONFIG_PATH_SEPARATOR
),
536 wxT("should be a relative path") );
540 config
->SetPath(path
);
545 void wxFontMapperBase::RestorePath(const wxString
& pathOld
)
547 GetConfig()->SetPath(pathOld
);
552 // ----------------------------------------------------------------------------
553 // charset/encoding correspondence
554 // ----------------------------------------------------------------------------
557 wxFontMapperBase::CharsetToEncoding(const wxString
& charset
,
558 bool WXUNUSED(interactive
))
560 int enc
= NonInteractiveCharsetToEncoding(charset
);
561 if ( enc
== wxFONTENCODING_UNKNOWN
)
563 // we should return wxFONTENCODING_SYSTEM from here for unknown
565 enc
= wxFONTENCODING_SYSTEM
;
568 return (wxFontEncoding
)enc
;
572 wxFontMapperBase::NonInteractiveCharsetToEncoding(const wxString
& charset
)
574 wxFontEncoding encoding
= wxFONTENCODING_SYSTEM
;
576 // we're going to modify it, make a copy
577 wxString cs
= charset
;
579 #if wxUSE_CONFIG && wxUSE_FILECONFIG
580 // first try the user-defined settings
581 wxFontMapperPathChanger
path(this, FONTMAPPER_CHARSET_PATH
);
584 wxConfigBase
*config
= GetConfig();
586 // do we have an encoding for this charset?
587 long value
= config
->Read(charset
, -1l);
590 if ( value
== wxFONTENCODING_UNKNOWN
)
592 // don't try to find it, in particular don't ask the user
596 if ( value
>= 0 && value
<= wxFONTENCODING_MAX
)
598 encoding
= (wxFontEncoding
)value
;
602 wxLogDebug(wxT("corrupted config data: invalid encoding %ld for charset '%s' ignored"),
603 value
, charset
.c_str());
607 if ( encoding
== wxFONTENCODING_SYSTEM
)
609 // may be we have an alias?
610 config
->SetPath(FONTMAPPER_CHARSET_ALIAS_PATH
);
612 wxString alias
= config
->Read(charset
);
613 if ( !alias
.empty() )
615 // yes, we do - use it instead
620 #endif // wxUSE_CONFIG
622 // if didn't find it there, try to recognize it ourselves
623 if ( encoding
== wxFONTENCODING_SYSTEM
)
629 // discard the optional quotes
632 if ( cs
[0u] == wxT('"') && cs
.Last() == wxT('"') )
634 cs
= wxString(cs
.c_str(), cs
.length() - 1);
638 for ( size_t i
= 0; i
< WXSIZEOF(gs_encodingNames
); ++i
)
640 for ( const wxChar
* const* encName
= gs_encodingNames
[i
]; *encName
; ++encName
)
642 if ( cs
.CmpNoCase(*encName
) == 0 )
643 return gs_encodings
[i
];
649 if ( cs
.Left(3) == wxT("ISO") )
651 // the dash is optional (or, to be exact, it is not, but
652 // several broken programs "forget" it)
653 const wxChar
*p
= cs
.c_str() + 3;
654 if ( *p
== wxT('-') )
658 if ( wxSscanf(p
, wxT("8859-%u"), &value
) == 1 )
660 // make it 0 based and check that it is strictly positive in
661 // the process (no such thing as iso8859-0 encoding)
662 if ( (value
-- > 0) &&
663 (value
< wxFONTENCODING_ISO8859_MAX
-
664 wxFONTENCODING_ISO8859_1
) )
666 // it's a valid ISO8859 encoding
667 value
+= wxFONTENCODING_ISO8859_1
;
668 encoding
= (wxFontEncoding
)value
;
672 else if ( cs
.Left(4) == wxT("8859") )
674 const wxChar
*p
= cs
.c_str();
677 if ( wxSscanf(p
, wxT("8859-%u"), &value
) == 1 )
679 // make it 0 based and check that it is strictly positive in
680 // the process (no such thing as iso8859-0 encoding)
681 if ( (value
-- > 0) &&
682 (value
< wxFONTENCODING_ISO8859_MAX
-
683 wxFONTENCODING_ISO8859_1
) )
685 // it's a valid ISO8859 encoding
686 value
+= wxFONTENCODING_ISO8859_1
;
687 encoding
= (wxFontEncoding
)value
;
691 else // check for Windows charsets
694 if ( cs
.Left(7) == wxT("WINDOWS") )
698 else if ( cs
.Left(2) == wxT("CP") )
702 else // not a Windows encoding
709 const wxChar
*p
= cs
.c_str() + len
;
710 if ( *p
== wxT('-') )
714 if ( wxSscanf(p
, wxT("%u"), &value
) == 1 )
719 if ( value
< wxFONTENCODING_CP12_MAX
-
720 wxFONTENCODING_CP1250
)
722 // a valid Windows code page
723 value
+= wxFONTENCODING_CP1250
;
724 encoding
= (wxFontEncoding
)value
;
731 encoding
= wxFONTENCODING_CP866
;
735 encoding
= wxFONTENCODING_CP874
;
739 encoding
= wxFONTENCODING_CP932
;
743 encoding
= wxFONTENCODING_CP936
;
747 encoding
= wxFONTENCODING_CP949
;
751 encoding
= wxFONTENCODING_CP950
;
764 size_t wxFontMapperBase::GetSupportedEncodingsCount()
766 return WXSIZEOF(gs_encodings
);
770 wxFontEncoding
wxFontMapperBase::GetEncoding(size_t n
)
772 wxCHECK_MSG( n
< WXSIZEOF(gs_encodings
), wxFONTENCODING_SYSTEM
,
773 wxT("wxFontMapper::GetEncoding(): invalid index") );
775 return gs_encodings
[n
];
779 wxString
wxFontMapperBase::GetEncodingDescription(wxFontEncoding encoding
)
781 if ( encoding
== wxFONTENCODING_DEFAULT
)
783 return _("Default encoding");
786 const size_t count
= WXSIZEOF(gs_encodingDescs
);
788 for ( size_t i
= 0; i
< count
; i
++ )
790 if ( gs_encodings
[i
] == encoding
)
792 return wxGetTranslation(gs_encodingDescs
[i
]);
797 str
.Printf(_("Unknown encoding (%d)"), encoding
);
803 wxString
wxFontMapperBase::GetEncodingName(wxFontEncoding encoding
)
805 if ( encoding
== wxFONTENCODING_DEFAULT
)
810 const size_t count
= WXSIZEOF(gs_encodingNames
);
812 for ( size_t i
= 0; i
< count
; i
++ )
814 if ( gs_encodings
[i
] == encoding
)
816 return gs_encodingNames
[i
][0];
821 str
.Printf(_("unknown-%d"), encoding
);
827 const wxChar
** wxFontMapperBase::GetAllEncodingNames(wxFontEncoding encoding
)
829 static const wxChar
* const dummy
[] = { NULL
};
831 for ( size_t i
= 0; i
< WXSIZEOF(gs_encodingNames
); i
++ )
833 if ( gs_encodings
[i
] == encoding
)
835 return const_cast<const wxChar
**>(gs_encodingNames
[i
]);
839 return const_cast<const wxChar
**>(dummy
);
843 wxFontEncoding
wxFontMapperBase::GetEncodingFromName(const wxString
& name
)
845 const size_t count
= WXSIZEOF(gs_encodingNames
);
847 for ( size_t i
= 0; i
< count
; i
++ )
849 for ( const wxChar
* const* encName
= gs_encodingNames
[i
]; *encName
; ++encName
)
851 if ( name
.CmpNoCase(*encName
) == 0 )
852 return gs_encodings
[i
];
856 return wxFONTENCODING_MAX
;
859 #endif // wxUSE_FONTMAP