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