]> git.saurik.com Git - wxWidgets.git/blame - src/common/encconv.cpp
XRC needs wxXML, check it
[wxWidgets.git] / src / common / encconv.cpp
CommitLineData
c958260b
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: encconv.cpp
3// Purpose: wxEncodingConverter class for converting between different
4// font encodings
5// Author: Vaclav Slavik
6// Copyright: (c) 1999 Vaclav Slavik
65571936 7// Licence: wxWindows licence
c958260b
VS
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2 10#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c958260b
VS
11#pragma implementation "encconv.h"
12#endif
13
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
18 #pragma hdrstop
19#endif
20
1e6feb95
VZ
21#if wxUSE_FONTMAP
22
c958260b
VS
23#include "wx/encconv.h"
24
25#include <stdlib.h>
26
94fc5183 27// conversion tables, generated by scripts in $(WXWIN)/misc/unictabl:
8f9c25cc 28#if defined( __BORLANDC__ ) || defined(__DARWIN__)
0f8d6461 29 #include "../common/unictabl.inc"
94fc5183 30#else
0f8d6461 31 #include "unictabl.inc"
94fc5183 32#endif
c958260b 33
f6bcfd97 34#if wxUSE_WCHAR_T
0f8d6461 35 typedef wchar_t tchar;
f6bcfd97 36#else
0f8d6461 37 typedef char tchar;
1c193821
JS
38#endif
39
788a28b4 40#ifdef __WXMAC__
0f8d6461
VZ
41 #include <ATSUnicode.h>
42 #include <TextCommon.h>
43 #include <TextEncodingConverter.h>
788a28b4 44
0f8d6461
VZ
45 #include "wx/fontutil.h"
46 #include "wx/mac/private.h" // includes mac headers
788a28b4 47
0f8d6461
VZ
48 wxUint16 gMacEncodings[wxFONTENCODING_MACMAX-wxFONTENCODING_MACMIN+1][128] ;
49 bool gMacEncodingsInited[wxFONTENCODING_MACMAX-wxFONTENCODING_MACMIN+1] ;
50#endif
788a28b4 51
0f8d6461
VZ
52#ifdef __WXWINCE__
53 #include "wx/msw/wince/missing.h" // for bsearch()
788a28b4
SC
54#endif
55
0f8d6461 56static wxUint16* GetEncTable(wxFontEncoding enc)
c958260b 57{
788a28b4 58#ifdef __WXMAC__
3af5821c 59 if( enc >= wxFONTENCODING_MACMIN && enc <= wxFONTENCODING_MACMAX )
788a28b4 60 {
3af5821c
SC
61 int i = enc-wxFONTENCODING_MACMIN ;
62 if ( gMacEncodingsInited[i] == false )
788a28b4 63 {
3af5821c
SC
64 TECObjectRef converter ;
65 TextEncodingBase code = wxMacGetSystemEncFromFontEnc( enc ) ;
1a18887b
WS
66 TextEncodingBase unicode = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode16BitFormat) ;
67 OSStatus status = TECCreateConverter(&converter,code,unicode);
68 char s[2] ;
69 s[1] = 0 ;
70 ByteCount byteInLen, byteOutLen ;
71 for( unsigned char c = 255 ; c >= 128 ; --c )
72 {
73 s[0] = c ;
3af5821c
SC
74 status = TECConvertText(converter, (ConstTextPtr) &s , 1, &byteInLen,
75 (TextPtr) &gMacEncodings[i][c-128] , 2, &byteOutLen);
1a18887b 76 }
3af5821c 77 status = TECDisposeConverter(converter);
1a18887b 78 gMacEncodingsInited[i]=true;
788a28b4 79 }
3af5821c 80 return gMacEncodings[i] ;
788a28b4
SC
81 }
82#endif
83
c958260b
VS
84 for (int i = 0; encodings_list[i].table != NULL; i++)
85 {
03424b1b 86 if (encodings_list[i].encoding == enc)
c958260b
VS
87 return encodings_list[i].table;
88 }
89 return NULL;
90}
91
92typedef struct {
93 wxUint16 u;
94 wxUint8 c;
95} CharsetItem;
96
0f8d6461
VZ
97extern "C" int wxCMPFUNC_CONV
98CompareCharsetItems(const void *i1, const void *i2)
c958260b
VS
99{
100 return ( ((CharsetItem*)i1) -> u - ((CharsetItem*)i2) -> u );
101}
102
103
0f8d6461 104static CharsetItem* BuildReverseTable(wxUint16 *tbl)
c958260b
VS
105{
106 CharsetItem *rev = new CharsetItem[128];
03424b1b 107
c958260b
VS
108 for (int i = 0; i < 128; i++)
109 rev[i].c = 128 + i, rev[i].u = tbl[i];
110
111 qsort(rev, 128, sizeof(CharsetItem), CompareCharsetItems);
03424b1b 112
c958260b
VS
113 return rev;
114}
115
116
117
118wxEncodingConverter::wxEncodingConverter()
119{
120 m_Table = NULL;
1a18887b
WS
121 m_UnicodeInput = m_UnicodeOutput = false;
122 m_JustCopy = false;
c958260b
VS
123}
124
125
126
127bool wxEncodingConverter::Init(wxFontEncoding input_enc, wxFontEncoding output_enc, int method)
128{
129 unsigned i;
999836aa 130 wxUint16 *in_tbl, *out_tbl = NULL;
c958260b
VS
131
132 if (m_Table) {delete[] m_Table; m_Table = NULL;}
133
f6bcfd97 134#if !wxUSE_WCHAR_T
1a18887b 135 if (input_enc == wxFONTENCODING_UNICODE || output_enc == wxFONTENCODING_UNICODE) return false;
c958260b
VS
136#endif
137
1a18887b 138 if (input_enc == output_enc) {m_JustCopy = true; return true;}
03424b1b 139
47e55c2f 140 m_UnicodeOutput = (output_enc == wxFONTENCODING_UNICODE);
1a18887b 141 m_JustCopy = false;
03424b1b 142
c958260b
VS
143 if (input_enc == wxFONTENCODING_UNICODE)
144 {
1a18887b 145 if ((out_tbl = GetEncTable(output_enc)) == NULL) return false;
c958260b 146
f6bcfd97
BP
147 m_Table = new tchar[65536];
148 for (i = 0; i < 128; i++) m_Table[i] = (tchar)i; // 7bit ASCII
03424b1b 149 for (i = 128; i < 65536; i++) m_Table[i] = (tchar)'?';
c958260b
VS
150 // FIXME - this should be character that means `unicode to charset' impossible, not '?'
151
152 if (method == wxCONVERT_SUBSTITUTE)
153 {
154 for (i = 0; i < encoding_unicode_fallback_count; i++)
f6bcfd97 155 m_Table[encoding_unicode_fallback[i].c] = (tchar) encoding_unicode_fallback[i].s;
c958260b
VS
156 }
157
158 for (i = 0; i < 128; i++)
f6bcfd97 159 m_Table[out_tbl[i]] = (tchar)(128 + i);
c958260b 160
1a18887b 161 m_UnicodeInput = true;
c958260b 162 }
b8c253ec 163 else // input !Unicode
c958260b 164 {
1a18887b 165 if ((in_tbl = GetEncTable(input_enc)) == NULL) return false;
c958260b 166 if (output_enc != wxFONTENCODING_UNICODE)
1a18887b 167 if ((out_tbl = GetEncTable(output_enc)) == NULL) return false;
c958260b 168
1a18887b 169 m_UnicodeInput = false;
03424b1b 170
f6bcfd97
BP
171 m_Table = new tchar[256];
172 for (i = 0; i < 128; i++) m_Table[i] = (tchar)i; // 7bit ASCII
03424b1b 173
c958260b
VS
174 if (output_enc == wxFONTENCODING_UNICODE)
175 {
f6bcfd97 176 for (i = 0; i < 128; i++) m_Table[128 + i] = (tchar)in_tbl[i];
1a18887b 177 return true;
c958260b 178 }
b8c253ec 179 else // output !Unicode
c958260b
VS
180 {
181 CharsetItem *rev = BuildReverseTable(out_tbl);
33ac7e6f
KB
182 CharsetItem *item;
183 CharsetItem key;
03424b1b
VZ
184
185 for (i = 0; i < 128; i++)
c958260b
VS
186 {
187 key.u = in_tbl[i];
188 item = (CharsetItem*) bsearch(&key, rev, 128, sizeof(CharsetItem), CompareCharsetItems);
189 if (item == NULL && method == wxCONVERT_SUBSTITUTE)
03424b1b 190 item = (CharsetItem*) bsearch(&key, encoding_unicode_fallback,
c958260b
VS
191 encoding_unicode_fallback_count, sizeof(CharsetItem), CompareCharsetItems);
192 if (item)
f6bcfd97 193 m_Table[128 + i] = (tchar)item -> c;
c958260b 194 else
33ac7e6f
KB
195#if wxUSE_WCHAR_T
196 m_Table[128 + i] = (wchar_t)(128 + i);
197#else
198 m_Table[128 + i] = (char)(128 + i);
307fd956 199#endif
c958260b 200 }
03424b1b 201
c958260b 202 delete[] rev;
c958260b
VS
203 }
204 }
b8c253ec 205
1a18887b 206 return true;
c958260b
VS
207}
208
209
47e55c2f 210
57c5293e 211void wxEncodingConverter::Convert(const char* input, char* output) const
c958260b 212{
f6bcfd97
BP
213 wxASSERT_MSG(!m_UnicodeOutput, wxT("You cannot convert to unicode if output is const char*!"));
214 wxASSERT_MSG(!m_UnicodeInput, wxT("You cannot convert from unicode if input is const char*!"));
215
216 const char *i;
217 char *o;
218
c958260b
VS
219 if (m_JustCopy)
220 {
f6bcfd97 221 strcpy(output, input);
c958260b
VS
222 return;
223 }
03424b1b 224
307fd956 225 wxCHECK_RET(m_Table != NULL, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
03424b1b 226
f6bcfd97
BP
227 for (i = input, o = output; *i != 0;)
228 *(o++) = (char)(m_Table[(wxUint8)*(i++)]);
c958260b
VS
229 *o = 0;
230}
231
232
f6bcfd97 233#if wxUSE_WCHAR_T
47e55c2f 234
57c5293e 235void wxEncodingConverter::Convert(const char* input, wchar_t* output) const
47e55c2f 236{
f6bcfd97 237 wxASSERT_MSG(m_UnicodeOutput, wxT("You cannot convert to 8-bit if output is const wchar_t*!"));
47e55c2f
VS
238 wxASSERT_MSG(!m_UnicodeInput, wxT("You cannot convert from unicode if input is const char*!"));
239
240 const char *i;
f6bcfd97 241 wchar_t *o;
47e55c2f
VS
242
243 if (m_JustCopy)
244 {
245 for (i = input, o = output; *i != 0;)
f6bcfd97 246 *(o++) = (wchar_t)(*(i++));
47e55c2f
VS
247 *o = 0;
248 return;
249 }
03424b1b 250
307fd956 251 wxCHECK_RET(m_Table != NULL, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
03424b1b 252
47e55c2f 253 for (i = input, o = output; *i != 0;)
f6bcfd97 254 *(o++) = (wchar_t)(m_Table[(wxUint8)*(i++)]);
47e55c2f
VS
255 *o = 0;
256}
257
258
259
57c5293e 260void wxEncodingConverter::Convert(const wchar_t* input, char* output) const
47e55c2f
VS
261{
262 wxASSERT_MSG(!m_UnicodeOutput, wxT("You cannot convert to unicode if output is const char*!"));
f6bcfd97 263 wxASSERT_MSG(m_UnicodeInput, wxT("You cannot convert from 8-bit if input is const wchar_t*!"));
47e55c2f 264
f6bcfd97 265 const wchar_t *i;
47e55c2f
VS
266 char *o;
267
268 if (m_JustCopy)
269 {
270 for (i = input, o = output; *i != 0;)
271 *(o++) = (char)(*(i++));
272 *o = 0;
273 return;
274 }
03424b1b 275
307fd956 276 wxCHECK_RET(m_Table != NULL, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
03424b1b 277
f6bcfd97
BP
278 for (i = input, o = output; *i != 0;)
279 *(o++) = (char)(m_Table[(wxUint16)*(i++)]);
47e55c2f
VS
280 *o = 0;
281}
282
283
284
57c5293e 285void wxEncodingConverter::Convert(const wchar_t* input, wchar_t* output) const
47e55c2f 286{
f6bcfd97
BP
287 wxASSERT_MSG(m_UnicodeOutput, wxT("You cannot convert to 8-bit if output is const wchar_t*!"));
288 wxASSERT_MSG(m_UnicodeInput, wxT("You cannot convert from 8-bit if input is const wchar_t*!"));
47e55c2f 289
f6bcfd97
BP
290 const wchar_t *i;
291 wchar_t *o;
47e55c2f
VS
292
293 if (m_JustCopy)
294 {
f6bcfd97
BP
295 // wcscpy() is not guaranteed to exist
296 for (i = input, o = output; *i != 0;)
297 *(o++) = (*(i++));
298 *o = 0;
47e55c2f
VS
299 return;
300 }
03424b1b 301
307fd956 302 wxCHECK_RET(m_Table != NULL, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
03424b1b 303
47e55c2f 304 for (i = input, o = output; *i != 0;)
f6bcfd97 305 *(o++) = (wchar_t)(m_Table[(wxUint8)*(i++)]);
47e55c2f
VS
306 *o = 0;
307}
308
f6bcfd97 309#endif // wxUSE_WCHAR_T
47e55c2f 310
c958260b 311
57c5293e 312wxString wxEncodingConverter::Convert(const wxString& input) const
c958260b
VS
313{
314 if (m_JustCopy) return input;
03424b1b 315
c958260b
VS
316 wxString s;
317 const wxChar *i;
03424b1b 318
4ccae30a
VZ
319 wxCHECK_MSG(m_Table != NULL, s,
320 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
03424b1b 321
c958260b 322 if (m_UnicodeInput)
307fd956 323 {
c958260b
VS
324 for (i = input.c_str(); *i != 0; i++)
325 s << (wxChar)(m_Table[(wxUint16)*i]);
307fd956 326 }
c958260b 327 else
307fd956 328 {
c958260b
VS
329 for (i = input.c_str(); *i != 0; i++)
330 s << (wxChar)(m_Table[(wxUint8)*i]);
307fd956
VZ
331 }
332
c958260b
VS
333 return s;
334}
335
336
337
338
47e55c2f
VS
339
340
341
c958260b 342// Following tables describe classes of encoding equivalence.
03424b1b 343//
c958260b
VS
344
345#define STOP wxFONTENCODING_SYSTEM
346
347#define NUM_OF_PLATFORMS 4 /*must conform to enum wxPLATFORM_XXXX !!!*/
617eb021 348#define ENC_PER_PLATFORM 5
c958260b 349 // max no. of encodings for one language used on one platform
617eb021 350 // Anybody thinks 5 is not enough? ;-)
c958260b 351
03424b1b 352static wxFontEncoding
c958260b
VS
353 EquivalentEncodings[][NUM_OF_PLATFORMS][ENC_PER_PLATFORM+1] = {
354
47e55c2f
VS
355 // *** Please put more common encodings as first! ***
356
03424b1b 357 // Western European
c958260b
VS
358 {
359 /* unix */ {wxFONTENCODING_ISO8859_1, wxFONTENCODING_ISO8859_15, STOP},
360 /* windows */ {wxFONTENCODING_CP1252, STOP},
361 /* os2 */ {STOP},
788a28b4 362 /* mac */ {wxFONTENCODING_MACROMAN, STOP}
c958260b
VS
363 },
364
47e55c2f 365 // Central European
c958260b
VS
366 {
367 /* unix */ {wxFONTENCODING_ISO8859_2, STOP},
368 /* windows */ {wxFONTENCODING_CP1250, STOP},
369 /* os2 */ {STOP},
788a28b4 370 /* mac */ {wxFONTENCODING_MACCENTRALEUR, STOP}
c958260b 371 },
03424b1b 372
47e55c2f
VS
373 // Baltic
374 {
03424b1b 375 /* unix */ {wxFONTENCODING_ISO8859_13, wxFONTENCODING_ISO8859_4, STOP},
47e55c2f
VS
376 /* windows */ {wxFONTENCODING_CP1257, STOP},
377 /* os2 */ {STOP},
03424b1b 378 /* mac */ {STOP}
47e55c2f
VS
379 },
380
381 // Hebrew
382 {
383 /* unix */ {wxFONTENCODING_ISO8859_8, STOP},
384 /* windows */ {wxFONTENCODING_CP1255, STOP},
385 /* os2 */ {STOP},
788a28b4 386 /* mac */ {wxFONTENCODING_MACHEBREW, STOP}
47e55c2f
VS
387 },
388
389 // Greek
390 {
391 /* unix */ {wxFONTENCODING_ISO8859_7, STOP},
392 /* windows */ {wxFONTENCODING_CP1253, STOP},
393 /* os2 */ {STOP},
788a28b4 394 /* mac */ {wxFONTENCODING_MACGREEK, STOP}
47e55c2f
VS
395 },
396
397 // Arabic
398 {
399 /* unix */ {wxFONTENCODING_ISO8859_6, STOP},
400 /* windows */ {wxFONTENCODING_CP1256, STOP},
401 /* os2 */ {STOP},
788a28b4 402 /* mac */ {wxFONTENCODING_MACARABIC, STOP}
47e55c2f
VS
403 },
404
405 // Turkish
406 {
407 /* unix */ {wxFONTENCODING_ISO8859_9, STOP},
408 /* windows */ {wxFONTENCODING_CP1254, STOP},
409 /* os2 */ {STOP},
788a28b4 410 /* mac */ {wxFONTENCODING_MACTURKISH, STOP}
47e55c2f
VS
411 },
412
413 // Cyrillic
3b61656e 414 {
15ad38c3 415 /* unix */ {wxFONTENCODING_KOI8, wxFONTENCODING_KOI8_U, wxFONTENCODING_ISO8859_5, STOP},
47e55c2f
VS
416 /* windows */ {wxFONTENCODING_CP1251, STOP},
417 /* os2 */ {STOP},
788a28b4 418 /* mac */ {wxFONTENCODING_MACCYRILLIC, STOP}
47e55c2f 419 },
c958260b
VS
420
421 {{STOP},{STOP},{STOP},{STOP}} /* Terminator */
422 /* no, _not_ Arnold! */
423};
424
425
df5168c4
MB
426static bool FindEncoding(const wxFontEncodingArray& arr, wxFontEncoding f)
427{
428 for (wxFontEncodingArray::const_iterator it = arr.begin(), en = arr.end();
429 it != en; ++it)
430 if (*it == f)
431 return true;
432 return false;
433}
c958260b
VS
434
435wxFontEncodingArray wxEncodingConverter::GetPlatformEquivalents(wxFontEncoding enc, int platform)
436{
437 if (platform == wxPLATFORM_CURRENT)
438 {
439#if defined(__WXMSW__)
440 platform = wxPLATFORM_WINDOWS;
441#elif defined(__WXGTK__) || defined(__WXMOTIF__)
442 platform = wxPLATFORM_UNIX;
443#elif defined(__WXOS2__)
444 platform = wxPLATFORM_OS2;
445#elif defined(__WXMAC__)
446 platform = wxPLATFORM_MAC;
447#endif
448 }
03424b1b 449
c958260b
VS
450 int i, clas, e ;
451 wxFontEncoding *f;
452 wxFontEncodingArray arr;
453
454 clas = 0;
455 while (EquivalentEncodings[clas][0][0] != STOP)
456 {
457 for (i = 0; i < NUM_OF_PLATFORMS; i++)
458 for (e = 0; EquivalentEncodings[clas][i][e] != STOP; e++)
459 if (EquivalentEncodings[clas][i][e] == enc)
460 {
47e55c2f 461 for (f = EquivalentEncodings[clas][platform]; *f != STOP; f++)
df5168c4 462 if (*f == enc) arr.push_back(enc);
47e55c2f 463 for (f = EquivalentEncodings[clas][platform]; *f != STOP; f++)
df5168c4 464 if (!FindEncoding(arr, *f)) arr.push_back(*f);
03424b1b 465 i = NUM_OF_PLATFORMS/*hack*/; break;
c958260b
VS
466 }
467 clas++;
468 }
03424b1b 469
c958260b
VS
470 return arr;
471}
472
473
474
475wxFontEncodingArray wxEncodingConverter::GetAllEquivalents(wxFontEncoding enc)
476{
477 int i, clas, e, j ;
478 wxFontEncoding *f;
479 wxFontEncodingArray arr;
03424b1b 480
47e55c2f 481 arr = GetPlatformEquivalents(enc); // we want them to be first items in array
c958260b
VS
482
483 clas = 0;
484 while (EquivalentEncodings[clas][0][0] != STOP)
485 {
486 for (i = 0; i < NUM_OF_PLATFORMS; i++)
487 for (e = 0; EquivalentEncodings[clas][i][e] != STOP; e++)
488 if (EquivalentEncodings[clas][i][e] == enc)
489 {
490 for (j = 0; j < NUM_OF_PLATFORMS; j++)
03424b1b 491 for (f = EquivalentEncodings[clas][j]; *f != STOP; f++)
df5168c4 492 if (!FindEncoding(arr, *f)) arr.push_back(*f);
03424b1b 493 i = NUM_OF_PLATFORMS/*hack*/; break;
c958260b
VS
494 }
495 clas++;
496 }
03424b1b 497
c958260b
VS
498 return arr;
499}
1e6feb95
VZ
500
501#endif // wxUSE_FONTMAP