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