1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxEncodingConverter class for converting between different
5 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "encconv.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
23 #include "wx/encconv.h"
27 // conversion tables, generated by scripts in $(WXWIN)/misc/unictabl:
28 #if defined( __BORLANDC__ ) || defined(__DARWIN__)
29 #include "../common/unictabl.inc"
31 #include "unictabl.inc"
35 typedef wchar_t tchar
;
41 #include <ATSUnicode.h>
42 #include <TextCommon.h>
43 #include <TextEncodingConverter.h>
45 #include "wx/fontutil.h"
46 #include "wx/mac/private.h" // includes mac headers
48 wxUint16 gMacEncodings
[wxFONTENCODING_MACMAX
-wxFONTENCODING_MACMIN
+1][128] ;
49 bool gMacEncodingsInited
[wxFONTENCODING_MACMAX
-wxFONTENCODING_MACMIN
+1] ;
53 #include "wx/msw/wince/missing.h" // for bsearch()
56 static wxUint16
* GetEncTable(wxFontEncoding enc
)
59 if( enc
>= wxFONTENCODING_MACMIN
&& enc
<= wxFONTENCODING_MACMAX
)
61 int i
= enc
-wxFONTENCODING_MACMIN
;
62 if ( gMacEncodingsInited
[i
] == false )
64 TECObjectRef converter
;
65 TextEncodingBase code
= wxMacGetSystemEncFromFontEnc( enc
) ;
66 TextEncodingBase unicode
= CreateTextEncoding(kTextEncodingUnicodeDefault
,0,kUnicode16BitFormat
) ;
67 OSStatus status
= TECCreateConverter(&converter
,code
,unicode
);
70 ByteCount byteInLen
, byteOutLen
;
71 for( unsigned char c
= 255 ; c
>= 128 ; --c
)
74 status
= TECConvertText(converter
, (ConstTextPtr
) &s
, 1, &byteInLen
,
75 (TextPtr
) &gMacEncodings
[i
][c
-128] , 2, &byteOutLen
);
77 status
= TECDisposeConverter(converter
);
78 gMacEncodingsInited
[i
]=true;
80 return gMacEncodings
[i
] ;
84 for (int i
= 0; encodings_list
[i
].table
!= NULL
; i
++)
86 if (encodings_list
[i
].encoding
== enc
)
87 return encodings_list
[i
].table
;
97 extern "C" int wxCMPFUNC_CONV
98 CompareCharsetItems(const void *i1
, const void *i2
)
100 return ( ((CharsetItem
*)i1
) -> u
- ((CharsetItem
*)i2
) -> u
);
104 static CharsetItem
* BuildReverseTable(wxUint16
*tbl
)
106 CharsetItem
*rev
= new CharsetItem
[128];
108 for (int i
= 0; i
< 128; i
++)
109 rev
[i
].c
= 128 + i
, rev
[i
].u
= tbl
[i
];
111 qsort(rev
, 128, sizeof(CharsetItem
), CompareCharsetItems
);
118 wxEncodingConverter::wxEncodingConverter()
121 m_UnicodeInput
= m_UnicodeOutput
= FALSE
;
127 bool wxEncodingConverter::Init(wxFontEncoding input_enc
, wxFontEncoding output_enc
, int method
)
130 wxUint16
*in_tbl
, *out_tbl
= NULL
;
132 if (m_Table
) {delete[] m_Table
; m_Table
= NULL
;}
135 if (input_enc
== wxFONTENCODING_UNICODE
|| output_enc
== wxFONTENCODING_UNICODE
) return FALSE
;
138 if (input_enc
== output_enc
) {m_JustCopy
= TRUE
; return TRUE
;}
140 m_UnicodeOutput
= (output_enc
== wxFONTENCODING_UNICODE
);
143 if (input_enc
== wxFONTENCODING_UNICODE
)
145 if ((out_tbl
= GetEncTable(output_enc
)) == NULL
) return FALSE
;
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 '?'
152 if (method
== wxCONVERT_SUBSTITUTE
)
154 for (i
= 0; i
< encoding_unicode_fallback_count
; i
++)
155 m_Table
[encoding_unicode_fallback
[i
].c
] = (tchar
) encoding_unicode_fallback
[i
].s
;
158 for (i
= 0; i
< 128; i
++)
159 m_Table
[out_tbl
[i
]] = (tchar
)(128 + i
);
161 m_UnicodeInput
= TRUE
;
163 else // input !Unicode
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
;
169 m_UnicodeInput
= FALSE
;
171 m_Table
= new tchar
[256];
172 for (i
= 0; i
< 128; i
++) m_Table
[i
] = (tchar
)i
; // 7bit ASCII
174 if (output_enc
== wxFONTENCODING_UNICODE
)
176 for (i
= 0; i
< 128; i
++) m_Table
[128 + i
] = (tchar
)in_tbl
[i
];
179 else // output !Unicode
181 CharsetItem
*rev
= BuildReverseTable(out_tbl
);
185 for (i
= 0; i
< 128; 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
);
193 m_Table
[128 + i
] = (tchar
)item
-> c
;
196 m_Table
[128 + i
] = (wchar_t)(128 + i
);
198 m_Table
[128 + i
] = (char)(128 + i
);
211 void wxEncodingConverter::Convert(const char* input
, char* output
) const
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*!"));
221 strcpy(output
, input
);
225 wxCHECK_RET(m_Table
!= NULL
, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
227 for (i
= input
, o
= output
; *i
!= 0;)
228 *(o
++) = (char)(m_Table
[(wxUint8
)*(i
++)]);
235 void wxEncodingConverter::Convert(const char* input
, wchar_t* output
) const
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*!"));
245 for (i
= input
, o
= output
; *i
!= 0;)
246 *(o
++) = (wchar_t)(*(i
++));
251 wxCHECK_RET(m_Table
!= NULL
, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
253 for (i
= input
, o
= output
; *i
!= 0;)
254 *(o
++) = (wchar_t)(m_Table
[(wxUint8
)*(i
++)]);
260 void wxEncodingConverter::Convert(const wchar_t* input
, char* output
) const
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*!"));
270 for (i
= input
, o
= output
; *i
!= 0;)
271 *(o
++) = (char)(*(i
++));
276 wxCHECK_RET(m_Table
!= NULL
, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
278 for (i
= input
, o
= output
; *i
!= 0;)
279 *(o
++) = (char)(m_Table
[(wxUint16
)*(i
++)]);
285 void wxEncodingConverter::Convert(const wchar_t* input
, wchar_t* output
) const
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*!"));
295 // wcscpy() is not guaranteed to exist
296 for (i
= input
, o
= output
; *i
!= 0;)
302 wxCHECK_RET(m_Table
!= NULL
, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
304 for (i
= input
, o
= output
; *i
!= 0;)
305 *(o
++) = (wchar_t)(m_Table
[(wxUint8
)*(i
++)]);
309 #endif // wxUSE_WCHAR_T
312 wxString
wxEncodingConverter::Convert(const wxString
& input
) const
314 if (m_JustCopy
) return input
;
319 wxCHECK_MSG(m_Table
!= NULL
, s
,
320 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
324 for (i
= input
.c_str(); *i
!= 0; i
++)
325 s
<< (wxChar
)(m_Table
[(wxUint16
)*i
]);
329 for (i
= input
.c_str(); *i
!= 0; i
++)
330 s
<< (wxChar
)(m_Table
[(wxUint8
)*i
]);
342 // Following tables describe classes of encoding equivalence.
345 #define STOP wxFONTENCODING_SYSTEM
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? ;-)
352 static wxFontEncoding
353 EquivalentEncodings
[][NUM_OF_PLATFORMS
][ENC_PER_PLATFORM
+1] = {
355 // *** Please put more common encodings as first! ***
359 /* unix */ {wxFONTENCODING_ISO8859_1
, wxFONTENCODING_ISO8859_15
, STOP
},
360 /* windows */ {wxFONTENCODING_CP1252
, STOP
},
362 /* mac */ {wxFONTENCODING_MACROMAN
, STOP
}
367 /* unix */ {wxFONTENCODING_ISO8859_2
, STOP
},
368 /* windows */ {wxFONTENCODING_CP1250
, STOP
},
370 /* mac */ {wxFONTENCODING_MACCENTRALEUR
, STOP
}
375 /* unix */ {wxFONTENCODING_ISO8859_13
, wxFONTENCODING_ISO8859_4
, STOP
},
376 /* windows */ {wxFONTENCODING_CP1257
, STOP
},
383 /* unix */ {wxFONTENCODING_ISO8859_8
, STOP
},
384 /* windows */ {wxFONTENCODING_CP1255
, STOP
},
386 /* mac */ {wxFONTENCODING_MACHEBREW
, STOP
}
391 /* unix */ {wxFONTENCODING_ISO8859_7
, STOP
},
392 /* windows */ {wxFONTENCODING_CP1253
, STOP
},
394 /* mac */ {wxFONTENCODING_MACGREEK
, STOP
}
399 /* unix */ {wxFONTENCODING_ISO8859_6
, STOP
},
400 /* windows */ {wxFONTENCODING_CP1256
, STOP
},
402 /* mac */ {wxFONTENCODING_MACARABIC
, STOP
}
407 /* unix */ {wxFONTENCODING_ISO8859_9
, STOP
},
408 /* windows */ {wxFONTENCODING_CP1254
, STOP
},
410 /* mac */ {wxFONTENCODING_MACTURKISH
, STOP
}
415 /* unix */ {wxFONTENCODING_KOI8
, wxFONTENCODING_KOI8_U
, wxFONTENCODING_ISO8859_5
, STOP
},
416 /* windows */ {wxFONTENCODING_CP1251
, STOP
},
418 /* mac */ {wxFONTENCODING_MACCYRILLIC
, STOP
}
421 {{STOP
},{STOP
},{STOP
},{STOP
}} /* Terminator */
422 /* no, _not_ Arnold! */
426 static bool FindEncoding(const wxFontEncodingArray
& arr
, wxFontEncoding f
)
428 for (wxFontEncodingArray::const_iterator it
= arr
.begin(), en
= arr
.end();
435 wxFontEncodingArray
wxEncodingConverter::GetPlatformEquivalents(wxFontEncoding enc
, int platform
)
437 if (platform
== wxPLATFORM_CURRENT
)
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
;
452 wxFontEncodingArray arr
;
455 while (EquivalentEncodings
[clas
][0][0] != STOP
)
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
)
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;
475 wxFontEncodingArray
wxEncodingConverter::GetAllEquivalents(wxFontEncoding enc
)
479 wxFontEncodingArray arr
;
481 arr
= GetPlatformEquivalents(enc
); // we want them to be first items in array
484 while (EquivalentEncodings
[clas
][0][0] != STOP
)
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
)
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;
501 #endif // wxUSE_FONTMAP