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