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