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