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
= wxUint8(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
)0;
151 if (method
== wxCONVERT_SUBSTITUTE
)
153 for (i
= 0; i
< encoding_unicode_fallback_count
; i
++)
154 m_Table
[encoding_unicode_fallback
[i
].c
] = (tchar
) encoding_unicode_fallback
[i
].s
;
157 for (i
= 0; i
< 128; i
++)
158 m_Table
[out_tbl
[i
]] = (tchar
)(128 + i
);
160 m_UnicodeInput
= true;
162 else // input !Unicode
164 if ((in_tbl
= GetEncTable(input_enc
)) == NULL
) return false;
165 if (output_enc
!= wxFONTENCODING_UNICODE
)
166 if ((out_tbl
= GetEncTable(output_enc
)) == NULL
) return false;
168 m_UnicodeInput
= false;
170 m_Table
= new tchar
[256];
171 for (i
= 0; i
< 128; i
++) m_Table
[i
] = (tchar
)i
; // 7bit ASCII
173 if (output_enc
== wxFONTENCODING_UNICODE
)
175 for (i
= 0; i
< 128; i
++) m_Table
[128 + i
] = (tchar
)in_tbl
[i
];
178 else // output !Unicode
180 CharsetItem
*rev
= BuildReverseTable(out_tbl
);
184 for (i
= 0; i
< 128; i
++)
187 item
= (CharsetItem
*) bsearch(&key
, rev
, 128, sizeof(CharsetItem
), CompareCharsetItems
);
188 if (item
== NULL
&& method
== wxCONVERT_SUBSTITUTE
)
189 item
= (CharsetItem
*) bsearch(&key
, encoding_unicode_fallback
,
190 encoding_unicode_fallback_count
, sizeof(CharsetItem
), CompareCharsetItems
);
192 m_Table
[128 + i
] = (tchar
)item
-> c
;
195 m_Table
[128 + i
] = (wchar_t)(128 + i
);
197 m_Table
[128 + i
] = (char)(128 + i
);
209 #define REPLACEMENT_CHAR ((tchar)'?')
211 inline tchar
GetTableValue(const tchar
*table
, tchar value
, bool& repl
)
213 tchar r
= table
[value
];
214 if (r
== 0 && value
!= 0)
216 r
= REPLACEMENT_CHAR
;
223 bool wxEncodingConverter::Convert(const char* input
, char* output
) const
225 wxASSERT_MSG(!m_UnicodeOutput
, wxT("You cannot convert to unicode if output is const char*!"));
226 wxASSERT_MSG(!m_UnicodeInput
, wxT("You cannot convert from unicode if input is const char*!"));
233 strcpy(output
, input
);
237 wxCHECK_MSG(m_Table
!= NULL
, false,
238 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
240 bool replaced
= false;
242 for (i
= input
, o
= output
; *i
!= 0;)
243 *(o
++) = (char)(GetTableValue(m_Table
, (wxUint8
)*(i
++), replaced
));
252 bool wxEncodingConverter::Convert(const char* input
, wchar_t* output
) const
254 wxASSERT_MSG(m_UnicodeOutput
, wxT("You cannot convert to 8-bit if output is const wchar_t*!"));
255 wxASSERT_MSG(!m_UnicodeInput
, wxT("You cannot convert from unicode if input is const char*!"));
262 for (i
= input
, o
= output
; *i
!= 0;)
263 *(o
++) = (wchar_t)(*(i
++));
268 wxCHECK_MSG(m_Table
!= NULL
, false,
269 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
271 bool replaced
= false;
273 for (i
= input
, o
= output
; *i
!= 0;)
274 *(o
++) = (wchar_t)(GetTableValue(m_Table
, (wxUint8
)*(i
++), replaced
));
282 bool wxEncodingConverter::Convert(const wchar_t* input
, char* output
) const
284 wxASSERT_MSG(!m_UnicodeOutput
, wxT("You cannot convert to unicode if output is const char*!"));
285 wxASSERT_MSG(m_UnicodeInput
, wxT("You cannot convert from 8-bit if input is const wchar_t*!"));
292 for (i
= input
, o
= output
; *i
!= 0;)
293 *(o
++) = (char)(*(i
++));
298 wxCHECK_MSG(m_Table
!= NULL
, false,
299 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
301 bool replaced
= false;
303 for (i
= input
, o
= output
; *i
!= 0;)
304 *(o
++) = (char)(GetTableValue(m_Table
, (wxUint16
)*(i
++), replaced
));
312 bool wxEncodingConverter::Convert(const wchar_t* input
, wchar_t* output
) const
314 wxASSERT_MSG(m_UnicodeOutput
, wxT("You cannot convert to 8-bit if output is const wchar_t*!"));
315 wxASSERT_MSG(m_UnicodeInput
, wxT("You cannot convert from 8-bit if input is const wchar_t*!"));
322 // wcscpy() is not guaranteed to exist
323 for (i
= input
, o
= output
; *i
!= 0;)
329 wxCHECK_MSG(m_Table
!= NULL
, false,
330 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
332 bool replaced
= false;
334 for (i
= input
, o
= output
; *i
!= 0;)
335 *(o
++) = (wchar_t)(GetTableValue(m_Table
, (wxUint8
)*(i
++), replaced
));
341 #endif // wxUSE_WCHAR_T
344 wxString
wxEncodingConverter::Convert(const wxString
& input
) const
346 if (m_JustCopy
) return input
;
351 wxCHECK_MSG(m_Table
!= NULL
, s
,
352 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
356 for (i
= input
.c_str(); *i
!= 0; i
++)
357 s
<< (wxChar
)(m_Table
[(wxUint16
)*i
]);
361 for (i
= input
.c_str(); *i
!= 0; i
++)
362 s
<< (wxChar
)(m_Table
[(wxUint8
)*i
]);
374 // Following tables describe classes of encoding equivalence.
377 #define STOP wxFONTENCODING_SYSTEM
379 #define NUM_OF_PLATFORMS 4 /*must conform to enum wxPLATFORM_XXXX !!!*/
380 #define ENC_PER_PLATFORM 5
381 // max no. of encodings for one language used on one platform
382 // Anybody thinks 5 is not enough? ;-)
384 static wxFontEncoding
385 EquivalentEncodings
[][NUM_OF_PLATFORMS
][ENC_PER_PLATFORM
+1] = {
387 // *** Please put more common encodings as first! ***
391 /* unix */ {wxFONTENCODING_ISO8859_1
, wxFONTENCODING_ISO8859_15
, STOP
},
392 /* windows */ {wxFONTENCODING_CP1252
, STOP
},
394 /* mac */ {wxFONTENCODING_MACROMAN
, STOP
}
399 /* unix */ {wxFONTENCODING_ISO8859_2
, STOP
},
400 /* windows */ {wxFONTENCODING_CP1250
, STOP
},
402 /* mac */ {wxFONTENCODING_MACCENTRALEUR
, STOP
}
407 /* unix */ {wxFONTENCODING_ISO8859_13
, wxFONTENCODING_ISO8859_4
, STOP
},
408 /* windows */ {wxFONTENCODING_CP1257
, STOP
},
415 /* unix */ {wxFONTENCODING_ISO8859_8
, STOP
},
416 /* windows */ {wxFONTENCODING_CP1255
, STOP
},
418 /* mac */ {wxFONTENCODING_MACHEBREW
, STOP
}
423 /* unix */ {wxFONTENCODING_ISO8859_7
, STOP
},
424 /* windows */ {wxFONTENCODING_CP1253
, STOP
},
426 /* mac */ {wxFONTENCODING_MACGREEK
, STOP
}
431 /* unix */ {wxFONTENCODING_ISO8859_6
, STOP
},
432 /* windows */ {wxFONTENCODING_CP1256
, STOP
},
434 /* mac */ {wxFONTENCODING_MACARABIC
, STOP
}
439 /* unix */ {wxFONTENCODING_ISO8859_9
, STOP
},
440 /* windows */ {wxFONTENCODING_CP1254
, STOP
},
442 /* mac */ {wxFONTENCODING_MACTURKISH
, STOP
}
447 /* unix */ {wxFONTENCODING_KOI8
, wxFONTENCODING_KOI8_U
, wxFONTENCODING_ISO8859_5
, STOP
},
448 /* windows */ {wxFONTENCODING_CP1251
, STOP
},
450 /* mac */ {wxFONTENCODING_MACCYRILLIC
, STOP
}
453 {{STOP
},{STOP
},{STOP
},{STOP
}} /* Terminator */
454 /* no, _not_ Arnold! */
458 static bool FindEncoding(const wxFontEncodingArray
& arr
, wxFontEncoding f
)
460 for (wxFontEncodingArray::const_iterator it
= arr
.begin(), en
= arr
.end();
467 wxFontEncodingArray
wxEncodingConverter::GetPlatformEquivalents(wxFontEncoding enc
, int platform
)
469 if (platform
== wxPLATFORM_CURRENT
)
471 #if defined(__WXMSW__)
472 platform
= wxPLATFORM_WINDOWS
;
473 #elif defined(__WXGTK__) || defined(__WXMOTIF__)
474 platform
= wxPLATFORM_UNIX
;
475 #elif defined(__WXOS2__)
476 platform
= wxPLATFORM_OS2
;
477 #elif defined(__WXMAC__)
478 platform
= wxPLATFORM_MAC
;
484 wxFontEncodingArray arr
;
487 while (EquivalentEncodings
[clas
][0][0] != STOP
)
489 for (i
= 0; i
< NUM_OF_PLATFORMS
; i
++)
490 for (e
= 0; EquivalentEncodings
[clas
][i
][e
] != STOP
; e
++)
491 if (EquivalentEncodings
[clas
][i
][e
] == enc
)
493 for (f
= EquivalentEncodings
[clas
][platform
]; *f
!= STOP
; f
++)
494 if (*f
== enc
) arr
.push_back(enc
);
495 for (f
= EquivalentEncodings
[clas
][platform
]; *f
!= STOP
; f
++)
496 if (!FindEncoding(arr
, *f
)) arr
.push_back(*f
);
497 i
= NUM_OF_PLATFORMS
/*hack*/; break;
507 wxFontEncodingArray
wxEncodingConverter::GetAllEquivalents(wxFontEncoding enc
)
511 wxFontEncodingArray arr
;
513 arr
= GetPlatformEquivalents(enc
); // we want them to be first items in array
516 while (EquivalentEncodings
[clas
][0][0] != STOP
)
518 for (i
= 0; i
< NUM_OF_PLATFORMS
; i
++)
519 for (e
= 0; EquivalentEncodings
[clas
][i
][e
] != STOP
; e
++)
520 if (EquivalentEncodings
[clas
][i
][e
] == enc
)
522 for (j
= 0; j
< NUM_OF_PLATFORMS
; j
++)
523 for (f
= EquivalentEncodings
[clas
][j
]; *f
!= STOP
; f
++)
524 if (!FindEncoding(arr
, *f
)) arr
.push_back(*f
);
525 i
= NUM_OF_PLATFORMS
/*hack*/; break;
533 #endif // wxUSE_FONTMAP