]> git.saurik.com Git - wxWidgets.git/blame - src/common/fmapbase.cpp
XML import corrections
[wxWidgets.git] / src / common / fmapbase.cpp
CommitLineData
e2478fde 1///////////////////////////////////////////////////////////////////////////////
02761f6c 2// Name: src/common/fmapbase.cpp
e2478fde
VZ
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>
526954c5 9// Licence: wxWindows licence
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"
02761f6c 33 #include "wx/module.h"
2523e9b7 34 #include "wx/wxcrtvararg.h"
e2478fde
VZ
35#endif //WX_PRECOMP
36
1c193821 37#if defined(__WXMSW__)
02761f6c
WS
38 #include "wx/msw/private.h" // includes windows.h for LOGFONT
39 #include "wx/msw/winundef.h"
1c193821
JS
40#endif
41
e2478fde
VZ
42#include "wx/fontmap.h"
43#include "wx/fmappriv.h"
44
45#include "wx/apptrait.h"
e2478fde 46
4676948b
JS
47// wxMemoryConfig uses wxFileConfig
48#if wxUSE_CONFIG && wxUSE_FILECONFIG
e2478fde
VZ
49 #include "wx/config.h"
50 #include "wx/memconf.h"
51#endif
52
53// ----------------------------------------------------------------------------
54// constants
55// ----------------------------------------------------------------------------
56
57// encodings supported by GetEncodingDescription
a243da29 58static const wxFontEncoding gs_encodings[] =
e2478fde
VZ
59{
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,
75 wxFONTENCODING_KOI8,
15ad38c3 76 wxFONTENCODING_KOI8_U,
69e087c1 77 wxFONTENCODING_CP866,
3c832d58 78 wxFONTENCODING_CP874,
e2478fde
VZ
79 wxFONTENCODING_CP932,
80 wxFONTENCODING_CP936,
81 wxFONTENCODING_CP949,
82 wxFONTENCODING_CP950,
83 wxFONTENCODING_CP1250,
84 wxFONTENCODING_CP1251,
85 wxFONTENCODING_CP1252,
86 wxFONTENCODING_CP1253,
87 wxFONTENCODING_CP1254,
88 wxFONTENCODING_CP1255,
89 wxFONTENCODING_CP1256,
90 wxFONTENCODING_CP1257,
91 wxFONTENCODING_CP437,
92 wxFONTENCODING_UTF7,
93 wxFONTENCODING_UTF8,
c91830cb
VZ
94 wxFONTENCODING_UTF16BE,
95 wxFONTENCODING_UTF16LE,
c91830cb
VZ
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,
a220ccb3 103 wxFONTENCODING_ISO2022_JP,
ba9e5c4d 104
03647350 105 wxFONTENCODING_MACROMAN,
ba9e5c4d
SC
106 wxFONTENCODING_MACJAPANESE,
107 wxFONTENCODING_MACCHINESETRAD,
108 wxFONTENCODING_MACKOREAN,
109 wxFONTENCODING_MACARABIC,
110 wxFONTENCODING_MACHEBREW,
111 wxFONTENCODING_MACGREEK,
112 wxFONTENCODING_MACCYRILLIC,
113 wxFONTENCODING_MACDEVANAGARI,
114 wxFONTENCODING_MACGURMUKHI,
115 wxFONTENCODING_MACGUJARATI,
116 wxFONTENCODING_MACORIYA,
117 wxFONTENCODING_MACBENGALI,
118 wxFONTENCODING_MACTAMIL,
119 wxFONTENCODING_MACTELUGU,
120 wxFONTENCODING_MACKANNADA,
121 wxFONTENCODING_MACMALAJALAM,
122 wxFONTENCODING_MACSINHALESE,
123 wxFONTENCODING_MACBURMESE,
124 wxFONTENCODING_MACKHMER,
125 wxFONTENCODING_MACTHAI,
126 wxFONTENCODING_MACLAOTIAN,
127 wxFONTENCODING_MACGEORGIAN,
128 wxFONTENCODING_MACARMENIAN,
129 wxFONTENCODING_MACCHINESESIMP,
130 wxFONTENCODING_MACTIBETAN,
131 wxFONTENCODING_MACMONGOLIAN,
132 wxFONTENCODING_MACETHIOPIC,
133 wxFONTENCODING_MACCENTRALEUR,
134 wxFONTENCODING_MACVIATNAMESE,
135 wxFONTENCODING_MACARABICEXT,
136 wxFONTENCODING_MACSYMBOL,
137 wxFONTENCODING_MACDINGBATS,
138 wxFONTENCODING_MACTURKISH,
139 wxFONTENCODING_MACCROATIAN,
140 wxFONTENCODING_MACICELANDIC,
141 wxFONTENCODING_MACROMANIAN,
142 wxFONTENCODING_MACCELTIC,
143 wxFONTENCODING_MACGAELIC,
144 wxFONTENCODING_MACKEYBOARD
e2478fde
VZ
145};
146
147// the descriptions for them
a243da29 148static const char* const gs_encodingDescs[] =
e2478fde
VZ
149{
150 wxTRANSLATE( "Western European (ISO-8859-1)" ),
151 wxTRANSLATE( "Central European (ISO-8859-2)" ),
152 wxTRANSLATE( "Esperanto (ISO-8859-3)" ),
153 wxTRANSLATE( "Baltic (old) (ISO-8859-4)" ),
154 wxTRANSLATE( "Cyrillic (ISO-8859-5)" ),
155 wxTRANSLATE( "Arabic (ISO-8859-6)" ),
156 wxTRANSLATE( "Greek (ISO-8859-7)" ),
157 wxTRANSLATE( "Hebrew (ISO-8859-8)" ),
158 wxTRANSLATE( "Turkish (ISO-8859-9)" ),
159 wxTRANSLATE( "Nordic (ISO-8859-10)" ),
160 wxTRANSLATE( "Thai (ISO-8859-11)" ),
161 wxTRANSLATE( "Indian (ISO-8859-12)" ),
162 wxTRANSLATE( "Baltic (ISO-8859-13)" ),
163 wxTRANSLATE( "Celtic (ISO-8859-14)" ),
164 wxTRANSLATE( "Western European with Euro (ISO-8859-15)" ),
165 wxTRANSLATE( "KOI8-R" ),
15ad38c3 166 wxTRANSLATE( "KOI8-U" ),
69e087c1 167 wxTRANSLATE( "Windows/DOS OEM Cyrillic (CP 866)" ),
3c832d58 168 wxTRANSLATE( "Windows Thai (CP 874)" ),
e2478fde
VZ
169 wxTRANSLATE( "Windows Japanese (CP 932)" ),
170 wxTRANSLATE( "Windows Chinese Simplified (CP 936)" ),
171 wxTRANSLATE( "Windows Korean (CP 949)" ),
172 wxTRANSLATE( "Windows Chinese Traditional (CP 950)" ),
173 wxTRANSLATE( "Windows Central European (CP 1250)" ),
174 wxTRANSLATE( "Windows Cyrillic (CP 1251)" ),
175 wxTRANSLATE( "Windows Western European (CP 1252)" ),
176 wxTRANSLATE( "Windows Greek (CP 1253)" ),
177 wxTRANSLATE( "Windows Turkish (CP 1254)" ),
178 wxTRANSLATE( "Windows Hebrew (CP 1255)" ),
179 wxTRANSLATE( "Windows Arabic (CP 1256)" ),
180 wxTRANSLATE( "Windows Baltic (CP 1257)" ),
181 wxTRANSLATE( "Windows/DOS OEM (CP 437)" ),
182 wxTRANSLATE( "Unicode 7 bit (UTF-7)" ),
183 wxTRANSLATE( "Unicode 8 bit (UTF-8)" ),
e4ccedf7 184#ifdef WORDS_BIGENDIAN
c91830cb 185 wxTRANSLATE( "Unicode 16 bit (UTF-16)" ),
c91830cb
VZ
186 wxTRANSLATE( "Unicode 16 bit Little Endian (UTF-16LE)" ),
187 wxTRANSLATE( "Unicode 32 bit (UTF-32)" ),
c91830cb 188 wxTRANSLATE( "Unicode 32 bit Little Endian (UTF-32LE)" ),
e4ccedf7
VZ
189#else // WORDS_BIGENDIAN
190 wxTRANSLATE( "Unicode 16 bit Big Endian (UTF-16BE)" ),
191 wxTRANSLATE( "Unicode 16 bit (UTF-16)" ),
192 wxTRANSLATE( "Unicode 32 bit Big Endian (UTF-32BE)" ),
193 wxTRANSLATE( "Unicode 32 bit (UTF-32)" ),
194#endif // WORDS_BIGENDIAN
e2478fde 195 wxTRANSLATE( "Extended Unix Codepage for Japanese (EUC-JP)" ),
8b3eb85d
VZ
196 wxTRANSLATE( "US-ASCII" ),
197 wxTRANSLATE( "BIG5" ),
198 wxTRANSLATE( "SHIFT-JIS" ),
199 wxTRANSLATE( "GB-2312" ),
a220ccb3 200 wxTRANSLATE( "ISO-2022-JP" ),
ba9e5c4d
SC
201
202 wxTRANSLATE( "MacRoman" ),
203 wxTRANSLATE( "MacJapanese" ),
204 wxTRANSLATE( "MacChineseTrad" ),
205 wxTRANSLATE( "MacKorean" ),
206 wxTRANSLATE( "MacArabic" ),
207 wxTRANSLATE( "MacHebrew" ),
208 wxTRANSLATE( "MacGreek" ),
209 wxTRANSLATE( "MacCyrillic" ),
210 wxTRANSLATE( "MacDevanagari" ),
211 wxTRANSLATE( "MacGurmukhi" ),
212 wxTRANSLATE( "MacGujarati" ),
213 wxTRANSLATE( "MacOriya" ),
214 wxTRANSLATE( "MacBengali" ),
215 wxTRANSLATE( "MacTamil" ),
216 wxTRANSLATE( "MacTelugu" ),
217 wxTRANSLATE( "MacKannada" ),
218 wxTRANSLATE( "MacMalayalam" ),
219 wxTRANSLATE( "MacSinhalese" ),
220 wxTRANSLATE( "MacBurmese" ),
221 wxTRANSLATE( "MacKhmer" ),
222 wxTRANSLATE( "MacThai" ),
223 wxTRANSLATE( "MacLaotian" ),
224 wxTRANSLATE( "MacGeorgian" ),
225 wxTRANSLATE( "MacArmenian" ),
226 wxTRANSLATE( "MacChineseSimp" ),
227 wxTRANSLATE( "MacTibetan" ),
228 wxTRANSLATE( "MacMongolian" ),
229 wxTRANSLATE( "MacEthiopic" ),
230 wxTRANSLATE( "MacCentralEurRoman" ),
231 wxTRANSLATE( "MacVietnamese" ),
232 wxTRANSLATE( "MacExtArabic" ),
233 wxTRANSLATE( "MacSymbol" ),
234 wxTRANSLATE( "MacDingbats" ),
235 wxTRANSLATE( "MacTurkish" ),
236 wxTRANSLATE( "MacCroatian" ),
237 wxTRANSLATE( "MacIcelandic" ),
238 wxTRANSLATE( "MacRomanian" ),
239 wxTRANSLATE( "MacCeltic" ),
240 wxTRANSLATE( "MacGaelic" ),
241 wxTRANSLATE( "MacKeyboardGlyphs" )
e2478fde
VZ
242};
243
244// and the internal names (these are not translated on purpose!)
a243da29 245static const wxChar* const gs_encodingNames[WXSIZEOF(gs_encodingDescs)][9] =
e2478fde 246{
e524af66
VZ
247 // names from the columns correspond to these OS:
248 // Linux Solaris and IRIX HP-UX AIX
9a83f860
VZ
249 { wxT("ISO-8859-1"), wxT("ISO8859-1"), wxT("iso88591"), wxT("8859-1"), wxT("iso_8859_1"), NULL },
250 { wxT("ISO-8859-2"), wxT("ISO8859-2"), wxT("iso88592"), wxT("8859-2"), NULL },
251 { wxT("ISO-8859-3"), wxT("ISO8859-3"), wxT("iso88593"), wxT("8859-3"), NULL },
252 { wxT("ISO-8859-4"), wxT("ISO8859-4"), wxT("iso88594"), wxT("8859-4"), NULL },
253 { wxT("ISO-8859-5"), wxT("ISO8859-5"), wxT("iso88595"), wxT("8859-5"), NULL },
254 { wxT("ISO-8859-6"), wxT("ISO8859-6"), wxT("iso88596"), wxT("8859-6"), NULL },
255 { wxT("ISO-8859-7"), wxT("ISO8859-7"), wxT("iso88597"), wxT("8859-7"), NULL },
256 { wxT("ISO-8859-8"), wxT("ISO8859-8"), wxT("iso88598"), wxT("8859-8"), NULL },
257 { wxT("ISO-8859-9"), wxT("ISO8859-9"), wxT("iso88599"), wxT("8859-9"), NULL },
258 { wxT("ISO-8859-10"), wxT("ISO8859-10"), wxT("iso885910"), wxT("8859-10"), NULL },
259 { wxT("ISO-8859-11"), wxT("ISO8859-11"), wxT("iso885911"), wxT("8859-11"), NULL },
260 { wxT("ISO-8859-12"), wxT("ISO8859-12"), wxT("iso885912"), wxT("8859-12"), NULL },
261 { wxT("ISO-8859-13"), wxT("ISO8859-13"), wxT("iso885913"), wxT("8859-13"), NULL },
262 { wxT("ISO-8859-14"), wxT("ISO8859-14"), wxT("iso885914"), wxT("8859-14"), NULL },
263 { wxT("ISO-8859-15"), wxT("ISO8859-15"), wxT("iso885915"), wxT("8859-15"), NULL },
761952d9 264
8b3eb85d
VZ
265 // although koi8-ru is not strictly speaking the same as koi8-r,
266 // they are similar enough to make mapping it to koi8 better than
267 // not recognizing it at all
761952d9
VZ
268 { wxT( "KOI8-R" ), wxT( "KOI8-RU" ), NULL },
269 { wxT( "KOI8-U" ), NULL },
270
69e087c1
VZ
271 { wxT( "WINDOWS-866" ), wxT( "CP866" ), NULL },
272
55f7a9ec
VZ
273 { wxT( "WINDOWS-874" ), wxT( "CP874" ), wxT( "MS874" ), wxT( "IBM-874" ), NULL },
274 { wxT( "WINDOWS-932" ), wxT( "CP932" ), wxT( "MS932" ), wxT( "IBM-932" ), NULL },
275 { wxT( "WINDOWS-936" ), wxT( "CP936" ), wxT( "MS936" ), wxT( "IBM-936" ), NULL },
276 { wxT( "WINDOWS-949" ), wxT( "CP949" ), wxT( "MS949" ), wxT( "IBM-949" ), wxT( "EUC-KR" ), wxT( "eucKR" ), wxT( "euc_kr" ), NULL },
277 { wxT( "WINDOWS-950" ), wxT( "CP950" ), wxT( "MS950" ), wxT( "IBM-950" ), NULL },
278 { wxT( "WINDOWS-1250" ),wxT( "CP1250" ),wxT( "MS1250" ),wxT( "IBM-1250" ),NULL },
279 { wxT( "WINDOWS-1251" ),wxT( "CP1251" ),wxT( "MS1251" ),wxT( "IBM-1251" ),NULL },
280 { wxT( "WINDOWS-1252" ),wxT( "CP1252" ),wxT( "MS1252" ),wxT( "IBM-1252" ),NULL },
281 { wxT( "WINDOWS-1253" ),wxT( "CP1253" ),wxT( "MS1253" ),wxT( "IBM-1253" ),NULL },
282 { wxT( "WINDOWS-1254" ),wxT( "CP1254" ),wxT( "MS1254" ),wxT( "IBM-1254" ),NULL },
283 { wxT( "WINDOWS-1255" ),wxT( "CP1255" ),wxT( "MS1255" ),wxT( "IBM-1255" ),NULL },
284 { wxT( "WINDOWS-1256" ),wxT( "CP1256" ),wxT( "MS1256" ),wxT( "IBM-1256" ),NULL },
285 { wxT( "WINDOWS-1257" ),wxT( "CP1257" ),wxT( "MS1257" ),wxT( "IBM-1257" ),NULL },
286 { wxT( "WINDOWS-437" ), wxT( "CP437" ), wxT( "MS437" ), wxT( "IBM-437" ), NULL },
761952d9 287
893bf460
VZ
288 { wxT( "UTF-7" ), wxT("UTF7"), NULL },
289 { wxT( "UTF-8" ), wxT("UTF8"), NULL },
e4ccedf7 290#ifdef WORDS_BIGENDIAN
893bf460
VZ
291 { wxT( "UTF-16BE" ), wxT("UTF16BE"), wxT("UCS-2BE"), wxT("UCS2BE"), wxT("UTF-16"), wxT("UTF16"), wxT("UCS-2"), wxT("UCS2"), NULL },
292 { wxT( "UTF-16LE" ), wxT("UTF16LE"), wxT("UCS-2LE"), wxT("UCS2LE"), NULL },
293 { wxT( "UTF-32BE" ), wxT("UTF32BE"), wxT("UCS-4BE" ), wxT("UTF-32"), wxT("UTF32"), wxT("UCS-4"), wxT("UCS4"), NULL },
294 { wxT( "UTF-32LE" ), wxT("UTF32LE"), wxT("UCS-4LE"), wxT("UCS4LE"), NULL },
e4ccedf7 295#else // WORDS_BIGENDIAN
893bf460
VZ
296 { wxT("UTF-16BE"), wxT("UTF16BE"), wxT("UCS-2BE"), wxT("UCS2BE"), NULL },
297 { wxT("UTF-16LE"), wxT("UTF16LE"), wxT("UCS-2LE"), wxT("UTF-16"), wxT("UTF16"), wxT("UCS-2"), wxT("UCS2"), NULL },
298 { wxT("UTF-32BE"), wxT("UTF32BE"), wxT("UCS-4BE"), wxT("UCS4BE"), NULL },
299 { wxT("UTF-32LE"), wxT("UTF32LE"), wxT("UCS-4LE"), wxT("UCS4LE"), wxT("UTF-32"), wxT("UTF32"), wxT("UCS-4"), wxT("UCS4"), NULL },
e4ccedf7 300#endif // WORDS_BIGENDIAN
761952d9
VZ
301
302 { wxT( "EUC-JP" ), wxT( "eucJP" ), wxT( "euc_jp" ), wxT( "IBM-eucJP" ), NULL },
303
3f0785cd
VZ
304 // 646 is for Solaris, roman8 -- for HP-UX
305 { wxT( "US-ASCII" ), wxT( "ASCII" ), wxT("C"), wxT("POSIX"), wxT("ANSI_X3.4-1968"),
306 wxT("646"), wxT("roman8"), wxT( "" ), NULL },
307
9ead8387 308 { wxT( "BIG5" ), wxT("big5"), NULL },
cd5b511a 309 { wxT( "SJIS" ), wxT( "SHIFT-JIS" ), wxT( "SHIFT_JIS" ), NULL },
761952d9 310 { wxT( "GB2312" ), NULL },
a220ccb3 311 { wxT( "ISO-2022-JP" ), NULL },
ba9e5c4d 312
03647350 313
ba9e5c4d
SC
314 { wxT( "MacRoman" ), NULL },
315 { wxT( "MacJapanese" ), NULL },
316 { wxT( "MacChineseTrad" ), NULL },
317 { wxT( "MacKorean" ), NULL },
318 { wxT( "MacArabic" ), NULL },
319 { wxT( "MacHebrew" ), NULL },
320 { wxT( "MacGreek" ), NULL },
321 { wxT( "MacCyrillic" ), NULL },
322 { wxT( "MacDevanagari" ), NULL },
323 { wxT( "MacGurmukhi" ), NULL },
324 { wxT( "MacGujarati" ), NULL },
325 { wxT( "MacOriya" ), NULL },
326 { wxT( "MacBengali" ), NULL },
327 { wxT( "MacTamil" ), NULL },
328 { wxT( "MacTelugu" ), NULL },
329 { wxT( "MacKannada" ), NULL },
330 { wxT( "MacMalayalam" ), NULL },
331 { wxT( "MacSinhalese" ), NULL },
332 { wxT( "MacBurmese" ), NULL },
333 { wxT( "MacKhmer" ), NULL },
334 { wxT( "MacThai" ), NULL },
335 { wxT( "MacLaotian" ), NULL },
336 { wxT( "MacGeorgian" ), NULL },
337 { wxT( "MacArmenian" ), NULL },
338 { wxT( "MacChineseSimp" ), NULL },
339 { wxT( "MacTibetan" ), NULL },
340 { wxT( "MacMongolian" ), NULL },
341 { wxT( "MacEthiopic" ), NULL },
342 { wxT( "MacCentralEurRoman" ), NULL },
343 { wxT( "MacVietnamese" ), NULL },
344 { wxT( "MacExtArabic" ), NULL },
345 { wxT( "MacSymbol" ), NULL },
346 { wxT( "MacDingbats" ), NULL },
347 { wxT( "MacTurkish" ), NULL },
348 { wxT( "MacCroatian" ), NULL },
349 { wxT( "MacIcelandic" ), NULL },
350 { wxT( "MacRomanian" ), NULL },
351 { wxT( "MacCeltic" ), NULL },
352 { wxT( "MacGaelic" ), NULL },
353 { wxT( "MacKeyboardGlyphs" ), NULL }
e2478fde
VZ
354};
355
8b3eb85d
VZ
356wxCOMPILE_TIME_ASSERT( WXSIZEOF(gs_encodingDescs) == WXSIZEOF(gs_encodings), EncodingsArraysNotInSync );
357wxCOMPILE_TIME_ASSERT( WXSIZEOF(gs_encodingNames) == WXSIZEOF(gs_encodings), EncodingsArraysNotInSync );
e2478fde
VZ
358
359// ----------------------------------------------------------------------------
360// private classes
361// ----------------------------------------------------------------------------
362
363// clean up the font mapper object
364class wxFontMapperModule : public wxModule
365{
366public:
367 wxFontMapperModule() : wxModule() { }
d5bfbd9a
VZ
368
369 virtual bool OnInit()
370 {
371 // a dummy wxFontMapperBase object could have been created during the
372 // program startup before wxApp was created, we have to delete it to
373 // allow creating the real font mapper next time it is needed now that
374 // we can create it (when the modules are initialized, wxApp object
375 // already exists)
376 wxFontMapperBase *fm = wxFontMapperBase::Get();
377 if ( fm && fm->IsDummy() )
378 wxFontMapperBase::Reset();
379
380 return true;
381 }
382
383 virtual void OnExit()
384 {
385 wxFontMapperBase::Reset();
386 }
e2478fde
VZ
387
388 DECLARE_DYNAMIC_CLASS(wxFontMapperModule)
389};
390
391IMPLEMENT_DYNAMIC_CLASS(wxFontMapperModule, wxModule)
392
393
394// ============================================================================
395// wxFontMapperBase implementation
396// ============================================================================
397
398wxFontMapper *wxFontMapperBase::sm_instance = NULL;
399
400// ----------------------------------------------------------------------------
401// ctor and dtor
402// ----------------------------------------------------------------------------
403
404wxFontMapperBase::wxFontMapperBase()
405{
4676948b 406#if wxUSE_CONFIG && wxUSE_FILECONFIG
5bbca8b0 407 m_configDummy = NULL;
e2478fde
VZ
408#endif // wxUSE_CONFIG
409}
410
411wxFontMapperBase::~wxFontMapperBase()
412{
4676948b 413#if wxUSE_CONFIG && wxUSE_FILECONFIG
5bbca8b0
KH
414 if ( m_configDummy )
415 delete m_configDummy;
e2478fde
VZ
416#endif // wxUSE_CONFIG
417}
418
419/* static */
73302af6 420wxFontMapperBase *wxFontMapperBase::Get()
e2478fde
VZ
421{
422 if ( !sm_instance )
423 {
424 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
425 if ( traits )
426 {
427 sm_instance = traits->CreateFontMapper();
428
429 wxASSERT_MSG( sm_instance,
9a83f860 430 wxT("wxAppTraits::CreateFontMapper() failed") );
e2478fde
VZ
431 }
432
433 if ( !sm_instance )
434 {
435 // last resort: we must create something because the existing code
436 // relies on always having a valid font mapper object
437 sm_instance = (wxFontMapper *)new wxFontMapperBase;
438 }
439 }
440
73302af6 441 return (wxFontMapperBase*)sm_instance;
e2478fde
VZ
442}
443
444/* static */
445wxFontMapper *wxFontMapperBase::Set(wxFontMapper *mapper)
446{
447 wxFontMapper *old = sm_instance;
448 sm_instance = mapper;
449 return old;
450}
d5bfbd9a
VZ
451
452/* static */
453void wxFontMapperBase::Reset()
454{
455 if ( sm_instance )
456 {
457 // we need a cast as wxFontMapper is not fully declared here and so the
458 // compiler can't know that it derives from wxFontMapperBase (but
459 // run-time behaviour will be correct because the dtor is virtual)
7fdf4140 460 delete (wxFontMapperBase *)sm_instance;
d5bfbd9a
VZ
461 sm_instance = NULL;
462 }
463}
e2478fde 464
4676948b 465#if wxUSE_CONFIG && wxUSE_FILECONFIG
e2478fde
VZ
466
467// ----------------------------------------------------------------------------
468// config usage customisation
469// ----------------------------------------------------------------------------
470
75ce4cb1
VS
471
472static wxString gs_defaultConfigPath(FONTMAPPER_ROOT_PATH);
473
e2478fde 474/* static */
75ce4cb1 475const wxString& wxFontMapperBase::GetDefaultConfigPath()
e2478fde 476{
75ce4cb1
VS
477 // NB: we return const wxString& and not wxString for compatibility
478 // with 2.8 that returned const wxChar*
479 return gs_defaultConfigPath;
e2478fde
VZ
480}
481
482void wxFontMapperBase::SetConfigPath(const wxString& prefix)
483{
02761f6c 484 wxCHECK_RET( !prefix.empty() && prefix[0] == wxCONFIG_PATH_SEPARATOR,
e2478fde
VZ
485 wxT("an absolute path should be given to wxFontMapper::SetConfigPath()") );
486
487 m_configRootPath = prefix;
488}
489
490// ----------------------------------------------------------------------------
491// get config object and path for it
492// ----------------------------------------------------------------------------
493
494wxConfigBase *wxFontMapperBase::GetConfig()
495{
5bbca8b0 496 wxConfigBase *config = wxConfig::Get(false);
e2478fde 497
5bbca8b0
KH
498 // If there is no global configuration, use an internal memory configuration
499 if ( !config )
e2478fde 500 {
5bbca8b0
KH
501 if ( !m_configDummy )
502 m_configDummy = new wxMemoryConfig;
503 config = m_configDummy;
504
505 // FIXME: ideally, we should add keys from dummy config to a real one later,
e2478fde
VZ
506 // but it is a low-priority task because typical wxWin application
507 // either doesn't use wxConfig at all or creates wxConfig object in
508 // wxApp::OnInit(), before any real interaction with the user takes
509 // place...
510 }
511
5bbca8b0 512 return config;
e2478fde
VZ
513}
514
515const wxString& wxFontMapperBase::GetConfigPath()
516{
517 if ( !m_configRootPath )
518 {
519 // use the default
520 m_configRootPath = GetDefaultConfigPath();
521 }
522
523 return m_configRootPath;
524}
525
526// ----------------------------------------------------------------------------
527// config helpers
528// ----------------------------------------------------------------------------
529
530bool wxFontMapperBase::ChangePath(const wxString& pathNew, wxString *pathOld)
531{
532 wxConfigBase *config = GetConfig();
533 if ( !config )
a62848fd 534 return false;
e2478fde
VZ
535
536 *pathOld = config->GetPath();
537
538 wxString path = GetConfigPath();
02761f6c 539 if ( path.empty() || path.Last() != wxCONFIG_PATH_SEPARATOR )
e2478fde
VZ
540 {
541 path += wxCONFIG_PATH_SEPARATOR;
542 }
543
544 wxASSERT_MSG( !pathNew || (pathNew[0] != wxCONFIG_PATH_SEPARATOR),
545 wxT("should be a relative path") );
546
547 path += pathNew;
548
549 config->SetPath(path);
550
a62848fd 551 return true;
e2478fde
VZ
552}
553
554void wxFontMapperBase::RestorePath(const wxString& pathOld)
555{
556 GetConfig()->SetPath(pathOld);
557}
558
f1c75e0f
JS
559#endif
560
e2478fde
VZ
561// ----------------------------------------------------------------------------
562// charset/encoding correspondence
563// ----------------------------------------------------------------------------
564
565wxFontEncoding
566wxFontMapperBase::CharsetToEncoding(const wxString& charset,
567 bool WXUNUSED(interactive))
4dc55027
VZ
568{
569 int enc = NonInteractiveCharsetToEncoding(charset);
570 if ( enc == wxFONTENCODING_UNKNOWN )
571 {
572 // we should return wxFONTENCODING_SYSTEM from here for unknown
573 // encodings
574 enc = wxFONTENCODING_SYSTEM;
575 }
576
577 return (wxFontEncoding)enc;
578}
579
580int
581wxFontMapperBase::NonInteractiveCharsetToEncoding(const wxString& charset)
e2478fde
VZ
582{
583 wxFontEncoding encoding = wxFONTENCODING_SYSTEM;
584
585 // we're going to modify it, make a copy
586 wxString cs = charset;
587
4676948b 588#if wxUSE_CONFIG && wxUSE_FILECONFIG
e2478fde
VZ
589 // first try the user-defined settings
590 wxFontMapperPathChanger path(this, FONTMAPPER_CHARSET_PATH);
591 if ( path.IsOk() )
592 {
593 wxConfigBase *config = GetConfig();
594
595 // do we have an encoding for this charset?
596 long value = config->Read(charset, -1l);
597 if ( value != -1 )
598 {
599 if ( value == wxFONTENCODING_UNKNOWN )
600 {
601 // don't try to find it, in particular don't ask the user
4dc55027 602 return value;
e2478fde
VZ
603 }
604
605 if ( value >= 0 && value <= wxFONTENCODING_MAX )
606 {
607 encoding = (wxFontEncoding)value;
608 }
609 else
610 {
611 wxLogDebug(wxT("corrupted config data: invalid encoding %ld for charset '%s' ignored"),
612 value, charset.c_str());
613 }
614 }
615
616 if ( encoding == wxFONTENCODING_SYSTEM )
617 {
618 // may be we have an alias?
619 config->SetPath(FONTMAPPER_CHARSET_ALIAS_PATH);
620
621 wxString alias = config->Read(charset);
02761f6c 622 if ( !alias.empty() )
e2478fde
VZ
623 {
624 // yes, we do - use it instead
625 cs = alias;
626 }
627 }
628 }
629#endif // wxUSE_CONFIG
630
631 // if didn't find it there, try to recognize it ourselves
632 if ( encoding == wxFONTENCODING_SYSTEM )
633 {
634 // trim any spaces
635 cs.Trim(true);
636 cs.Trim(false);
637
638 // discard the optional quotes
639 if ( !cs.empty() )
640 {
9a83f860 641 if ( cs[0u] == wxT('"') && cs.Last() == wxT('"') )
e2478fde
VZ
642 {
643 cs = wxString(cs.c_str(), cs.length() - 1);
644 }
645 }
646
893bf460
VZ
647 for ( size_t i = 0; i < WXSIZEOF(gs_encodingNames); ++i )
648 {
a243da29 649 for ( const wxChar* const* encName = gs_encodingNames[i]; *encName; ++encName )
893bf460
VZ
650 {
651 if ( cs.CmpNoCase(*encName) == 0 )
652 return gs_encodings[i];
653 }
654 }
8b3eb85d
VZ
655
656 cs.MakeUpper();
657
658 if ( cs.Left(3) == wxT("ISO") )
e2478fde 659 {
893bf460
VZ
660 // the dash is optional (or, to be exact, it is not, but
661 // several broken programs "forget" it)
e2478fde
VZ
662 const wxChar *p = cs.c_str() + 3;
663 if ( *p == wxT('-') )
664 p++;
a62848fd 665
e2478fde
VZ
666 unsigned int value;
667 if ( wxSscanf(p, wxT("8859-%u"), &value) == 1 )
668 {
e2478fde
VZ
669 // make it 0 based and check that it is strictly positive in
670 // the process (no such thing as iso8859-0 encoding)
671 if ( (value-- > 0) &&
672 (value < wxFONTENCODING_ISO8859_MAX -
673 wxFONTENCODING_ISO8859_1) )
674 {
675 // it's a valid ISO8859 encoding
676 value += wxFONTENCODING_ISO8859_1;
677 encoding = (wxFontEncoding)value;
678 }
679 }
680 }
681 else if ( cs.Left(4) == wxT("8859") )
682 {
683 const wxChar *p = cs.c_str();
a62848fd 684
e2478fde
VZ
685 unsigned int value;
686 if ( wxSscanf(p, wxT("8859-%u"), &value) == 1 )
687 {
e2478fde
VZ
688 // make it 0 based and check that it is strictly positive in
689 // the process (no such thing as iso8859-0 encoding)
690 if ( (value-- > 0) &&
691 (value < wxFONTENCODING_ISO8859_MAX -
692 wxFONTENCODING_ISO8859_1) )
693 {
694 // it's a valid ISO8859 encoding
695 value += wxFONTENCODING_ISO8859_1;
696 encoding = (wxFontEncoding)value;
697 }
698 }
699 }
700 else // check for Windows charsets
701 {
702 size_t len;
703 if ( cs.Left(7) == wxT("WINDOWS") )
704 {
705 len = 7;
706 }
707 else if ( cs.Left(2) == wxT("CP") )
708 {
709 len = 2;
710 }
711 else // not a Windows encoding
712 {
713 len = 0;
714 }
715
716 if ( len )
717 {
718 const wxChar *p = cs.c_str() + len;
719 if ( *p == wxT('-') )
720 p++;
721
17a1ebd1 722 unsigned int value;
e2478fde
VZ
723 if ( wxSscanf(p, wxT("%u"), &value) == 1 )
724 {
725 if ( value >= 1250 )
726 {
727 value -= 1250;
728 if ( value < wxFONTENCODING_CP12_MAX -
729 wxFONTENCODING_CP1250 )
730 {
731 // a valid Windows code page
732 value += wxFONTENCODING_CP1250;
733 encoding = (wxFontEncoding)value;
734 }
735 }
736
737 switch ( value )
738 {
c4b4f847
VZ
739 case 866:
740 encoding = wxFONTENCODING_CP866;
741 break;
742
3c832d58
DS
743 case 874:
744 encoding = wxFONTENCODING_CP874;
745 break;
746
e2478fde
VZ
747 case 932:
748 encoding = wxFONTENCODING_CP932;
749 break;
750
751 case 936:
752 encoding = wxFONTENCODING_CP936;
753 break;
754
755 case 949:
756 encoding = wxFONTENCODING_CP949;
757 break;
758
759 case 950:
760 encoding = wxFONTENCODING_CP950;
761 break;
762 }
763 }
764 }
765 }
766 //else: unknown
767 }
768
769 return encoding;
770}
771
772/* static */
773size_t wxFontMapperBase::GetSupportedEncodingsCount()
774{
775 return WXSIZEOF(gs_encodings);
776}
777
778/* static */
779wxFontEncoding wxFontMapperBase::GetEncoding(size_t n)
780{
781 wxCHECK_MSG( n < WXSIZEOF(gs_encodings), wxFONTENCODING_SYSTEM,
9a83f860 782 wxT("wxFontMapper::GetEncoding(): invalid index") );
e2478fde
VZ
783
784 return gs_encodings[n];
785}
786
787/* static */
788wxString wxFontMapperBase::GetEncodingDescription(wxFontEncoding encoding)
789{
790 if ( encoding == wxFONTENCODING_DEFAULT )
791 {
792 return _("Default encoding");
793 }
794
795 const size_t count = WXSIZEOF(gs_encodingDescs);
796
797 for ( size_t i = 0; i < count; i++ )
798 {
799 if ( gs_encodings[i] == encoding )
800 {
801 return wxGetTranslation(gs_encodingDescs[i]);
802 }
803 }
804
805 wxString str;
806 str.Printf(_("Unknown encoding (%d)"), encoding);
807
808 return str;
809}
810
811/* static */
812wxString wxFontMapperBase::GetEncodingName(wxFontEncoding encoding)
813{
814 if ( encoding == wxFONTENCODING_DEFAULT )
815 {
816 return _("default");
817 }
818
819 const size_t count = WXSIZEOF(gs_encodingNames);
820
821 for ( size_t i = 0; i < count; i++ )
822 {
823 if ( gs_encodings[i] == encoding )
824 {
8b3eb85d 825 return gs_encodingNames[i][0];
e2478fde
VZ
826 }
827 }
828
829 wxString str;
830 str.Printf(_("unknown-%d"), encoding);
831
832 return str;
833}
834
8b3eb85d
VZ
835/* static */
836const wxChar** wxFontMapperBase::GetAllEncodingNames(wxFontEncoding encoding)
837{
a243da29 838 static const wxChar* const dummy[] = { NULL };
8b3eb85d
VZ
839
840 for ( size_t i = 0; i < WXSIZEOF(gs_encodingNames); i++ )
841 {
842 if ( gs_encodings[i] == encoding )
843 {
a243da29 844 return const_cast<const wxChar**>(gs_encodingNames[i]);
8b3eb85d
VZ
845 }
846 }
847
a243da29 848 return const_cast<const wxChar**>(dummy);
8b3eb85d
VZ
849}
850
910b9fc5
VZ
851/* static */
852wxFontEncoding wxFontMapperBase::GetEncodingFromName(const wxString& name)
853{
854 const size_t count = WXSIZEOF(gs_encodingNames);
855
856 for ( size_t i = 0; i < count; i++ )
857 {
a243da29 858 for ( const wxChar* const* encName = gs_encodingNames[i]; *encName; ++encName )
910b9fc5 859 {
893bf460 860 if ( name.CmpNoCase(*encName) == 0 )
8b3eb85d 861 return gs_encodings[i];
910b9fc5
VZ
862 }
863 }
864
910b9fc5
VZ
865 return wxFONTENCODING_MAX;
866}
867
e2478fde 868#endif // wxUSE_FONTMAP