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