]>
Commit | Line | Data |
---|---|---|
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> | |
5283098e JS |
26 | #ifdef __WINE__ |
27 | #include <search.h> | |
28 | #endif | |
c958260b | 29 | |
94fc5183 VS |
30 | // conversion tables, generated by scripts in $(WXWIN)/misc/unictabl: |
31 | #ifdef __BORLANDC__ | |
32 | #include "../common/unictabl.inc" | |
33 | #else | |
03424b1b | 34 | #include "unictabl.inc" |
94fc5183 | 35 | #endif |
c958260b | 36 | |
f6bcfd97 BP |
37 | #if wxUSE_WCHAR_T |
38 | typedef wchar_t tchar; | |
39 | #else | |
40 | typedef char tchar; | |
41 | #endif | |
c958260b | 42 | |
eda22ec3 | 43 | static wxUint16* LINKAGEMODE GetEncTable(wxFontEncoding enc) |
c958260b VS |
44 | { |
45 | for (int i = 0; encodings_list[i].table != NULL; i++) | |
46 | { | |
03424b1b | 47 | if (encodings_list[i].encoding == enc) |
c958260b VS |
48 | return encodings_list[i].table; |
49 | } | |
50 | return NULL; | |
51 | } | |
52 | ||
53 | typedef struct { | |
54 | wxUint16 u; | |
55 | wxUint8 c; | |
56 | } CharsetItem; | |
57 | ||
58 | ||
59 | ||
90350682 | 60 | extern "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 | 66 | static 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 | ||
80 | wxEncodingConverter::wxEncodingConverter() | |
81 | { | |
82 | m_Table = NULL; | |
47e55c2f | 83 | m_UnicodeInput = m_UnicodeOutput = FALSE; |
c958260b VS |
84 | m_JustCopy = FALSE; |
85 | } | |
86 | ||
87 | ||
88 | ||
89 | bool 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; | |
124 | return TRUE; | |
125 | } | |
03424b1b | 126 | |
c958260b VS |
127 | else |
128 | { | |
129 | if ((in_tbl = GetEncTable(input_enc)) == NULL) return FALSE; | |
130 | if (output_enc != wxFONTENCODING_UNICODE) | |
131 | if ((out_tbl = GetEncTable(output_enc)) == NULL) return FALSE; | |
132 | ||
133 | m_UnicodeInput = FALSE; | |
03424b1b | 134 | |
f6bcfd97 BP |
135 | m_Table = new tchar[256]; |
136 | for (i = 0; i < 128; i++) m_Table[i] = (tchar)i; // 7bit ASCII | |
03424b1b | 137 | |
c958260b VS |
138 | if (output_enc == wxFONTENCODING_UNICODE) |
139 | { | |
f6bcfd97 | 140 | for (i = 0; i < 128; i++) m_Table[128 + i] = (tchar)in_tbl[i]; |
c958260b VS |
141 | return TRUE; |
142 | } | |
03424b1b | 143 | else |
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 VS |
166 | delete[] rev; |
167 | return TRUE; | |
168 | } | |
169 | } | |
170 | } | |
171 | ||
172 | ||
47e55c2f | 173 | |
f6bcfd97 | 174 | void wxEncodingConverter::Convert(const char* input, char* output) |
c958260b | 175 | { |
f6bcfd97 BP |
176 | wxASSERT_MSG(!m_UnicodeOutput, wxT("You cannot convert to unicode if output is const char*!")); |
177 | wxASSERT_MSG(!m_UnicodeInput, wxT("You cannot convert from unicode if input is const char*!")); | |
178 | ||
179 | const char *i; | |
180 | char *o; | |
181 | ||
c958260b VS |
182 | if (m_JustCopy) |
183 | { | |
f6bcfd97 | 184 | strcpy(output, input); |
c958260b VS |
185 | return; |
186 | } | |
03424b1b | 187 | |
307fd956 | 188 | wxCHECK_RET(m_Table != NULL, wxT("You must call wxEncodingConverter::Init() before actually converting!")); |
03424b1b | 189 | |
f6bcfd97 BP |
190 | for (i = input, o = output; *i != 0;) |
191 | *(o++) = (char)(m_Table[(wxUint8)*(i++)]); | |
c958260b VS |
192 | *o = 0; |
193 | } | |
194 | ||
195 | ||
f6bcfd97 | 196 | #if wxUSE_WCHAR_T |
47e55c2f | 197 | |
f6bcfd97 | 198 | void wxEncodingConverter::Convert(const char* input, wchar_t* output) |
47e55c2f | 199 | { |
f6bcfd97 | 200 | wxASSERT_MSG(m_UnicodeOutput, wxT("You cannot convert to 8-bit if output is const wchar_t*!")); |
47e55c2f VS |
201 | wxASSERT_MSG(!m_UnicodeInput, wxT("You cannot convert from unicode if input is const char*!")); |
202 | ||
203 | const char *i; | |
f6bcfd97 | 204 | wchar_t *o; |
47e55c2f VS |
205 | |
206 | if (m_JustCopy) | |
207 | { | |
208 | for (i = input, o = output; *i != 0;) | |
f6bcfd97 | 209 | *(o++) = (wchar_t)(*(i++)); |
47e55c2f VS |
210 | *o = 0; |
211 | return; | |
212 | } | |
03424b1b | 213 | |
307fd956 | 214 | wxCHECK_RET(m_Table != NULL, wxT("You must call wxEncodingConverter::Init() before actually converting!")); |
03424b1b | 215 | |
47e55c2f | 216 | for (i = input, o = output; *i != 0;) |
f6bcfd97 | 217 | *(o++) = (wchar_t)(m_Table[(wxUint8)*(i++)]); |
47e55c2f VS |
218 | *o = 0; |
219 | } | |
220 | ||
221 | ||
222 | ||
f6bcfd97 | 223 | void wxEncodingConverter::Convert(const wchar_t* input, char* output) |
47e55c2f VS |
224 | { |
225 | wxASSERT_MSG(!m_UnicodeOutput, wxT("You cannot convert to unicode if output is const char*!")); | |
f6bcfd97 | 226 | wxASSERT_MSG(m_UnicodeInput, wxT("You cannot convert from 8-bit if input is const wchar_t*!")); |
47e55c2f | 227 | |
f6bcfd97 | 228 | const wchar_t *i; |
47e55c2f VS |
229 | char *o; |
230 | ||
231 | if (m_JustCopy) | |
232 | { | |
233 | for (i = input, o = output; *i != 0;) | |
234 | *(o++) = (char)(*(i++)); | |
235 | *o = 0; | |
236 | return; | |
237 | } | |
03424b1b | 238 | |
307fd956 | 239 | wxCHECK_RET(m_Table != NULL, wxT("You must call wxEncodingConverter::Init() before actually converting!")); |
03424b1b | 240 | |
f6bcfd97 BP |
241 | for (i = input, o = output; *i != 0;) |
242 | *(o++) = (char)(m_Table[(wxUint16)*(i++)]); | |
47e55c2f VS |
243 | *o = 0; |
244 | } | |
245 | ||
246 | ||
247 | ||
f6bcfd97 | 248 | void wxEncodingConverter::Convert(const wchar_t* input, wchar_t* output) |
47e55c2f | 249 | { |
f6bcfd97 BP |
250 | wxASSERT_MSG(m_UnicodeOutput, wxT("You cannot convert to 8-bit if output is const wchar_t*!")); |
251 | wxASSERT_MSG(m_UnicodeInput, wxT("You cannot convert from 8-bit if input is const wchar_t*!")); | |
47e55c2f | 252 | |
f6bcfd97 BP |
253 | const wchar_t *i; |
254 | wchar_t *o; | |
47e55c2f VS |
255 | |
256 | if (m_JustCopy) | |
257 | { | |
f6bcfd97 BP |
258 | // wcscpy() is not guaranteed to exist |
259 | for (i = input, o = output; *i != 0;) | |
260 | *(o++) = (*(i++)); | |
261 | *o = 0; | |
47e55c2f VS |
262 | return; |
263 | } | |
03424b1b | 264 | |
307fd956 | 265 | wxCHECK_RET(m_Table != NULL, wxT("You must call wxEncodingConverter::Init() before actually converting!")); |
03424b1b | 266 | |
47e55c2f | 267 | for (i = input, o = output; *i != 0;) |
f6bcfd97 | 268 | *(o++) = (wchar_t)(m_Table[(wxUint8)*(i++)]); |
47e55c2f VS |
269 | *o = 0; |
270 | } | |
271 | ||
f6bcfd97 | 272 | #endif // wxUSE_WCHAR_T |
47e55c2f | 273 | |
c958260b VS |
274 | |
275 | wxString wxEncodingConverter::Convert(const wxString& input) | |
276 | { | |
277 | if (m_JustCopy) return input; | |
03424b1b | 278 | |
c958260b VS |
279 | wxString s; |
280 | const wxChar *i; | |
03424b1b | 281 | |
4ccae30a VZ |
282 | wxCHECK_MSG(m_Table != NULL, s, |
283 | wxT("You must call wxEncodingConverter::Init() before actually converting!")); | |
03424b1b | 284 | |
c958260b | 285 | if (m_UnicodeInput) |
307fd956 | 286 | { |
c958260b VS |
287 | for (i = input.c_str(); *i != 0; i++) |
288 | s << (wxChar)(m_Table[(wxUint16)*i]); | |
307fd956 | 289 | } |
c958260b | 290 | else |
307fd956 | 291 | { |
c958260b VS |
292 | for (i = input.c_str(); *i != 0; i++) |
293 | s << (wxChar)(m_Table[(wxUint8)*i]); | |
307fd956 VZ |
294 | } |
295 | ||
c958260b VS |
296 | return s; |
297 | } | |
298 | ||
299 | ||
300 | ||
301 | ||
47e55c2f VS |
302 | |
303 | ||
304 | ||
c958260b | 305 | // Following tables describe classes of encoding equivalence. |
03424b1b | 306 | // |
c958260b VS |
307 | |
308 | #define STOP wxFONTENCODING_SYSTEM | |
309 | ||
310 | #define NUM_OF_PLATFORMS 4 /*must conform to enum wxPLATFORM_XXXX !!!*/ | |
617eb021 | 311 | #define ENC_PER_PLATFORM 5 |
c958260b | 312 | // max no. of encodings for one language used on one platform |
617eb021 | 313 | // Anybody thinks 5 is not enough? ;-) |
c958260b | 314 | |
03424b1b | 315 | static wxFontEncoding |
c958260b VS |
316 | EquivalentEncodings[][NUM_OF_PLATFORMS][ENC_PER_PLATFORM+1] = { |
317 | ||
47e55c2f VS |
318 | // *** Please put more common encodings as first! *** |
319 | ||
03424b1b | 320 | // Western European |
c958260b VS |
321 | { |
322 | /* unix */ {wxFONTENCODING_ISO8859_1, wxFONTENCODING_ISO8859_15, STOP}, | |
323 | /* windows */ {wxFONTENCODING_CP1252, STOP}, | |
324 | /* os2 */ {STOP}, | |
03424b1b | 325 | /* mac */ {STOP} |
c958260b VS |
326 | }, |
327 | ||
47e55c2f | 328 | // Central European |
c958260b VS |
329 | { |
330 | /* unix */ {wxFONTENCODING_ISO8859_2, STOP}, | |
331 | /* windows */ {wxFONTENCODING_CP1250, STOP}, | |
332 | /* os2 */ {STOP}, | |
03424b1b | 333 | /* mac */ {STOP} |
c958260b | 334 | }, |
03424b1b | 335 | |
47e55c2f VS |
336 | // Baltic |
337 | { | |
03424b1b | 338 | /* unix */ {wxFONTENCODING_ISO8859_13, wxFONTENCODING_ISO8859_4, STOP}, |
47e55c2f VS |
339 | /* windows */ {wxFONTENCODING_CP1257, STOP}, |
340 | /* os2 */ {STOP}, | |
03424b1b | 341 | /* mac */ {STOP} |
47e55c2f VS |
342 | }, |
343 | ||
344 | // Hebrew | |
345 | { | |
346 | /* unix */ {wxFONTENCODING_ISO8859_8, STOP}, | |
347 | /* windows */ {wxFONTENCODING_CP1255, STOP}, | |
348 | /* os2 */ {STOP}, | |
03424b1b | 349 | /* mac */ {STOP} |
47e55c2f VS |
350 | }, |
351 | ||
352 | // Greek | |
353 | { | |
354 | /* unix */ {wxFONTENCODING_ISO8859_7, STOP}, | |
355 | /* windows */ {wxFONTENCODING_CP1253, STOP}, | |
356 | /* os2 */ {STOP}, | |
03424b1b | 357 | /* mac */ {STOP} |
47e55c2f VS |
358 | }, |
359 | ||
360 | // Arabic | |
361 | { | |
362 | /* unix */ {wxFONTENCODING_ISO8859_6, STOP}, | |
363 | /* windows */ {wxFONTENCODING_CP1256, STOP}, | |
364 | /* os2 */ {STOP}, | |
03424b1b | 365 | /* mac */ {STOP} |
47e55c2f VS |
366 | }, |
367 | ||
368 | // Turkish | |
369 | { | |
370 | /* unix */ {wxFONTENCODING_ISO8859_9, STOP}, | |
371 | /* windows */ {wxFONTENCODING_CP1254, STOP}, | |
372 | /* os2 */ {STOP}, | |
03424b1b | 373 | /* mac */ {STOP} |
47e55c2f VS |
374 | }, |
375 | ||
376 | // Cyrillic | |
3b61656e VS |
377 | { |
378 | /* unix */ {wxFONTENCODING_KOI8, wxFONTENCODING_ISO8859_5, STOP}, | |
47e55c2f VS |
379 | /* windows */ {wxFONTENCODING_CP1251, STOP}, |
380 | /* os2 */ {STOP}, | |
03424b1b | 381 | /* mac */ {STOP} |
47e55c2f | 382 | }, |
c958260b VS |
383 | |
384 | {{STOP},{STOP},{STOP},{STOP}} /* Terminator */ | |
385 | /* no, _not_ Arnold! */ | |
386 | }; | |
387 | ||
388 | ||
389 | ||
390 | ||
391 | wxFontEncodingArray wxEncodingConverter::GetPlatformEquivalents(wxFontEncoding enc, int platform) | |
392 | { | |
393 | if (platform == wxPLATFORM_CURRENT) | |
394 | { | |
395 | #if defined(__WXMSW__) | |
396 | platform = wxPLATFORM_WINDOWS; | |
397 | #elif defined(__WXGTK__) || defined(__WXMOTIF__) | |
398 | platform = wxPLATFORM_UNIX; | |
399 | #elif defined(__WXOS2__) | |
400 | platform = wxPLATFORM_OS2; | |
401 | #elif defined(__WXMAC__) | |
402 | platform = wxPLATFORM_MAC; | |
403 | #endif | |
404 | } | |
03424b1b | 405 | |
c958260b VS |
406 | int i, clas, e ; |
407 | wxFontEncoding *f; | |
408 | wxFontEncodingArray arr; | |
409 | ||
410 | clas = 0; | |
411 | while (EquivalentEncodings[clas][0][0] != STOP) | |
412 | { | |
413 | for (i = 0; i < NUM_OF_PLATFORMS; i++) | |
414 | for (e = 0; EquivalentEncodings[clas][i][e] != STOP; e++) | |
415 | if (EquivalentEncodings[clas][i][e] == enc) | |
416 | { | |
47e55c2f VS |
417 | for (f = EquivalentEncodings[clas][platform]; *f != STOP; f++) |
418 | if (*f == enc) arr.Add(enc); | |
419 | for (f = EquivalentEncodings[clas][platform]; *f != STOP; f++) | |
c958260b | 420 | if (arr.Index(*f) == wxNOT_FOUND) arr.Add(*f); |
03424b1b | 421 | i = NUM_OF_PLATFORMS/*hack*/; break; |
c958260b VS |
422 | } |
423 | clas++; | |
424 | } | |
03424b1b | 425 | |
c958260b VS |
426 | return arr; |
427 | } | |
428 | ||
429 | ||
430 | ||
431 | wxFontEncodingArray wxEncodingConverter::GetAllEquivalents(wxFontEncoding enc) | |
432 | { | |
433 | int i, clas, e, j ; | |
434 | wxFontEncoding *f; | |
435 | wxFontEncodingArray arr; | |
03424b1b | 436 | |
47e55c2f | 437 | arr = GetPlatformEquivalents(enc); // we want them to be first items in array |
c958260b VS |
438 | |
439 | clas = 0; | |
440 | while (EquivalentEncodings[clas][0][0] != STOP) | |
441 | { | |
442 | for (i = 0; i < NUM_OF_PLATFORMS; i++) | |
443 | for (e = 0; EquivalentEncodings[clas][i][e] != STOP; e++) | |
444 | if (EquivalentEncodings[clas][i][e] == enc) | |
445 | { | |
446 | for (j = 0; j < NUM_OF_PLATFORMS; j++) | |
03424b1b VZ |
447 | for (f = EquivalentEncodings[clas][j]; *f != STOP; f++) |
448 | if (arr.Index(*f) == wxNOT_FOUND) arr.Add(*f); | |
449 | i = NUM_OF_PLATFORMS/*hack*/; break; | |
c958260b VS |
450 | } |
451 | clas++; | |
452 | } | |
03424b1b | 453 | |
c958260b VS |
454 | return arr; |
455 | } | |
1e6feb95 VZ |
456 | |
457 | #endif // wxUSE_FONTMAP |