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