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