]> git.saurik.com Git - wxWidgets.git/blob - src/common/encconv.cpp
added missing include; minor reformatting
[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 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(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 wxUint16 *in_tbl, *out_tbl = NULL;
128
129 if (m_Table) {delete[] m_Table; m_Table = NULL;}
130
131 #if !wxUSE_WCHAR_T
132 if (input_enc == wxFONTENCODING_UNICODE || output_enc == wxFONTENCODING_UNICODE) return false;
133 #endif
134
135 if (input_enc == output_enc) {m_JustCopy = true; return true;}
136
137 m_UnicodeOutput = (output_enc == wxFONTENCODING_UNICODE);
138 m_JustCopy = false;
139
140 if (input_enc == wxFONTENCODING_UNICODE)
141 {
142 if ((out_tbl = GetEncTable(output_enc)) == NULL) return false;
143
144 m_Table = new tchar[65536];
145 for (i = 0; i < 128; i++) m_Table[i] = (tchar)i; // 7bit ASCII
146 for (i = 128; i < 65536; i++) m_Table[i] = (tchar)0;
147
148 if (method == wxCONVERT_SUBSTITUTE)
149 {
150 for (i = 0; i < encoding_unicode_fallback_count; i++)
151 m_Table[encoding_unicode_fallback[i].c] = (tchar) encoding_unicode_fallback[i].s;
152 }
153
154 for (i = 0; i < 128; i++)
155 m_Table[out_tbl[i]] = (tchar)(128 + i);
156
157 m_UnicodeInput = true;
158 }
159 else // input !Unicode
160 {
161 if ((in_tbl = GetEncTable(input_enc)) == NULL) return false;
162 if (output_enc != wxFONTENCODING_UNICODE)
163 if ((out_tbl = GetEncTable(output_enc)) == NULL) return false;
164
165 m_UnicodeInput = false;
166
167 m_Table = new tchar[256];
168 for (i = 0; i < 128; i++) m_Table[i] = (tchar)i; // 7bit ASCII
169
170 if (output_enc == wxFONTENCODING_UNICODE)
171 {
172 for (i = 0; i < 128; i++) m_Table[128 + i] = (tchar)in_tbl[i];
173 return true;
174 }
175 else // output !Unicode
176 {
177 CharsetItem *rev = BuildReverseTable(out_tbl);
178 CharsetItem *item;
179 CharsetItem key;
180
181 for (i = 0; i < 128; i++)
182 {
183 key.u = in_tbl[i];
184 item = (CharsetItem*) bsearch(&key, rev, 128, sizeof(CharsetItem), CompareCharsetItems);
185 if (item == NULL && method == wxCONVERT_SUBSTITUTE)
186 item = (CharsetItem*) bsearch(&key, encoding_unicode_fallback,
187 encoding_unicode_fallback_count, sizeof(CharsetItem), CompareCharsetItems);
188 if (item)
189 m_Table[128 + i] = (tchar)item -> c;
190 else
191 #if wxUSE_WCHAR_T
192 m_Table[128 + i] = (wchar_t)(128 + i);
193 #else
194 m_Table[128 + i] = (char)(128 + i);
195 #endif
196 }
197
198 delete[] rev;
199 }
200 }
201
202 return true;
203 }
204
205
206 #define REPLACEMENT_CHAR ((tchar)'?')
207
208 inline 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
220 bool wxEncodingConverter::Convert(const char* input, char* output) const
221 {
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
228 if (m_JustCopy)
229 {
230 strcpy(output, input);
231 return true;
232 }
233
234 wxCHECK_MSG(m_Table != NULL, false,
235 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
236
237 bool replaced = false;
238
239 for (i = input, o = output; *i != 0;)
240 *(o++) = (char)(GetTableValue(m_Table, (wxUint8)*(i++), replaced));
241 *o = 0;
242
243 return !replaced;
244 }
245
246
247 #if wxUSE_WCHAR_T
248
249 bool wxEncodingConverter::Convert(const char* input, wchar_t* output) const
250 {
251 wxASSERT_MSG(m_UnicodeOutput, wxT("You cannot convert to 8-bit if output is const wchar_t*!"));
252 wxASSERT_MSG(!m_UnicodeInput, wxT("You cannot convert from unicode if input is const char*!"));
253
254 const char *i;
255 wchar_t *o;
256
257 if (m_JustCopy)
258 {
259 for (i = input, o = output; *i != 0;)
260 *(o++) = (wchar_t)(*(i++));
261 *o = 0;
262 return true;
263 }
264
265 wxCHECK_MSG(m_Table != NULL, false,
266 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
267
268 bool replaced = false;
269
270 for (i = input, o = output; *i != 0;)
271 *(o++) = (wchar_t)(GetTableValue(m_Table, (wxUint8)*(i++), replaced));
272 *o = 0;
273
274 return !replaced;
275 }
276
277
278
279 bool wxEncodingConverter::Convert(const wchar_t* input, char* output) const
280 {
281 wxASSERT_MSG(!m_UnicodeOutput, wxT("You cannot convert to unicode if output is const char*!"));
282 wxASSERT_MSG(m_UnicodeInput, wxT("You cannot convert from 8-bit if input is const wchar_t*!"));
283
284 const wchar_t *i;
285 char *o;
286
287 if (m_JustCopy)
288 {
289 for (i = input, o = output; *i != 0;)
290 *(o++) = (char)(*(i++));
291 *o = 0;
292 return true;
293 }
294
295 wxCHECK_MSG(m_Table != NULL, false,
296 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
297
298 bool replaced = false;
299
300 for (i = input, o = output; *i != 0;)
301 *(o++) = (char)(GetTableValue(m_Table, (wxUint16)*(i++), replaced));
302 *o = 0;
303
304 return !replaced;
305 }
306
307
308
309 bool wxEncodingConverter::Convert(const wchar_t* input, wchar_t* output) const
310 {
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*!"));
313
314 const wchar_t *i;
315 wchar_t *o;
316
317 if (m_JustCopy)
318 {
319 // wcscpy() is not guaranteed to exist
320 for (i = input, o = output; *i != 0;)
321 *(o++) = (*(i++));
322 *o = 0;
323 return true;
324 }
325
326 wxCHECK_MSG(m_Table != NULL, false,
327 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
328
329 bool replaced = false;
330
331 for (i = input, o = output; *i != 0;)
332 *(o++) = (wchar_t)(GetTableValue(m_Table, (wxUint8)*(i++), replaced));
333 *o = 0;
334
335 return !replaced;
336 }
337
338 #endif // wxUSE_WCHAR_T
339
340
341 wxString wxEncodingConverter::Convert(const wxString& input) const
342 {
343 if (m_JustCopy) return input;
344
345 wxString s;
346 const wxChar *i;
347
348 wxCHECK_MSG(m_Table != NULL, s,
349 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
350
351 if (m_UnicodeInput)
352 {
353 for (i = input.c_str(); *i != 0; i++)
354 s << (wxChar)(m_Table[(wxUint16)*i]);
355 }
356 else
357 {
358 for (i = input.c_str(); *i != 0; i++)
359 s << (wxChar)(m_Table[(wxUint8)*i]);
360 }
361
362 return s;
363 }
364
365
366
367
368
369
370
371 // Following tables describe classes of encoding equivalence.
372 //
373
374 #define STOP wxFONTENCODING_SYSTEM
375
376 #define NUM_OF_PLATFORMS 4 /*must conform to enum wxPLATFORM_XXXX !!!*/
377 #define ENC_PER_PLATFORM 5
378 // max no. of encodings for one language used on one platform
379 // Anybody thinks 5 is not enough? ;-)
380
381 static wxFontEncoding
382 EquivalentEncodings[][NUM_OF_PLATFORMS][ENC_PER_PLATFORM+1] = {
383
384 // *** Please put more common encodings as first! ***
385
386 // Western European
387 {
388 /* unix */ {wxFONTENCODING_ISO8859_1, wxFONTENCODING_ISO8859_15, STOP},
389 /* windows */ {wxFONTENCODING_CP1252, STOP},
390 /* os2 */ {STOP},
391 /* mac */ {wxFONTENCODING_MACROMAN, STOP}
392 },
393
394 // Central European
395 {
396 /* unix */ {wxFONTENCODING_ISO8859_2, STOP},
397 /* windows */ {wxFONTENCODING_CP1250, STOP},
398 /* os2 */ {STOP},
399 /* mac */ {wxFONTENCODING_MACCENTRALEUR, STOP}
400 },
401
402 // Baltic
403 {
404 /* unix */ {wxFONTENCODING_ISO8859_13, wxFONTENCODING_ISO8859_4, STOP},
405 /* windows */ {wxFONTENCODING_CP1257, STOP},
406 /* os2 */ {STOP},
407 /* mac */ {STOP}
408 },
409
410 // Hebrew
411 {
412 /* unix */ {wxFONTENCODING_ISO8859_8, STOP},
413 /* windows */ {wxFONTENCODING_CP1255, STOP},
414 /* os2 */ {STOP},
415 /* mac */ {wxFONTENCODING_MACHEBREW, STOP}
416 },
417
418 // Greek
419 {
420 /* unix */ {wxFONTENCODING_ISO8859_7, STOP},
421 /* windows */ {wxFONTENCODING_CP1253, STOP},
422 /* os2 */ {STOP},
423 /* mac */ {wxFONTENCODING_MACGREEK, STOP}
424 },
425
426 // Arabic
427 {
428 /* unix */ {wxFONTENCODING_ISO8859_6, STOP},
429 /* windows */ {wxFONTENCODING_CP1256, STOP},
430 /* os2 */ {STOP},
431 /* mac */ {wxFONTENCODING_MACARABIC, STOP}
432 },
433
434 // Turkish
435 {
436 /* unix */ {wxFONTENCODING_ISO8859_9, STOP},
437 /* windows */ {wxFONTENCODING_CP1254, STOP},
438 /* os2 */ {STOP},
439 /* mac */ {wxFONTENCODING_MACTURKISH, STOP}
440 },
441
442 // Cyrillic
443 {
444 /* unix */ {wxFONTENCODING_KOI8, wxFONTENCODING_KOI8_U, wxFONTENCODING_ISO8859_5, STOP},
445 /* windows */ {wxFONTENCODING_CP1251, STOP},
446 /* os2 */ {STOP},
447 /* mac */ {wxFONTENCODING_MACCYRILLIC, STOP}
448 },
449
450 {{STOP},{STOP},{STOP},{STOP}} /* Terminator */
451 /* no, _not_ Arnold! */
452 };
453
454
455 static 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 }
463
464 wxFontEncodingArray 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;
472 #elif defined(__WXPM__)
473 platform = wxPLATFORM_OS2;
474 #elif defined(__WXMAC__)
475 platform = wxPLATFORM_MAC;
476 #endif
477 }
478
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 {
490 for (f = EquivalentEncodings[clas][platform]; *f != STOP; f++)
491 if (*f == enc) arr.push_back(enc);
492 for (f = EquivalentEncodings[clas][platform]; *f != STOP; f++)
493 if (!FindEncoding(arr, *f)) arr.push_back(*f);
494 i = NUM_OF_PLATFORMS/*hack*/; break;
495 }
496 clas++;
497 }
498
499 return arr;
500 }
501
502
503
504 wxFontEncodingArray wxEncodingConverter::GetAllEquivalents(wxFontEncoding enc)
505 {
506 int i, clas, e, j ;
507 wxFontEncoding *f;
508 wxFontEncodingArray arr;
509
510 arr = GetPlatformEquivalents(enc); // we want them to be first items in array
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++)
520 for (f = EquivalentEncodings[clas][j]; *f != STOP; f++)
521 if (!FindEncoding(arr, *f)) arr.push_back(*f);
522 i = NUM_OF_PLATFORMS/*hack*/; break;
523 }
524 clas++;
525 }
526
527 return arr;
528 }
529