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 #ifndef _WX_ENCCONV_H_
11 #define _WX_ENCCONV_H_
13 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma interface "encconv.h"
19 #include "wx/object.h"
20 #include "wx/fontenc.h"
21 #include "wx/dynarray.h"
23 // ----------------------------------------------------------------------------
25 // ----------------------------------------------------------------------------
36 wxPLATFORM_CURRENT
= -1,
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 WX_DEFINE_ARRAY_INT(wxFontEncoding
, wxFontEncodingArray
);
50 //--------------------------------------------------------------------------------
51 // wxEncodingConverter
52 // This class is capable of converting strings between any two
53 // 8bit encodings/charsets. It can also convert from/to Unicode
54 //--------------------------------------------------------------------------------
56 class WXDLLIMPEXP_BASE wxEncodingConverter
: public wxObject
60 wxEncodingConverter();
61 ~wxEncodingConverter() { if (m_Table
) delete[] m_Table
; }
63 // Initialize conversion. Both output or input encoding may
64 // be wxFONTENCODING_UNICODE, but only if wxUSE_WCHAR_T is set to 1.
66 // All subsequent calls to Convert() will interpret it's argument
67 // as a string in input_enc encoding and will output string in
68 // output_enc encoding.
70 // You must call this method before calling Convert. You may call
71 // it more than once in order to switch to another conversion
73 // Method affects behaviour of Convert() in case input character
74 // cannot be converted because it does not exist in output encoding:
75 // wxCONVERT_STRICT --
76 // follow behaviour of GNU Recode - just copy unconvertable
77 // characters to output and don't change them (it's integer
78 // value will stay the same)
79 // wxCONVERT_SUBSTITUTE --
80 // try some (lossy) substitutions - e.g. replace
81 // unconvertable latin capitals with acute by ordinary
82 // capitals, replace en-dash or em-dash by '-' etc.
83 // both modes gurantee that output string will have same length
86 // Returns false if given conversion is impossible, true otherwise
87 // (conversion may be impossible either if you try to convert
88 // to Unicode with non-Unicode build of wxWidgets or if input
89 // or output encoding is not supported.)
90 bool Init(wxFontEncoding input_enc
, wxFontEncoding output_enc
, int method
= wxCONVERT_STRICT
);
92 // Convert input string according to settings passed to Init.
93 // Note that you must call Init before using Convert!
94 bool Convert(const char* input
, char* output
) const;
95 bool Convert(char* str
) const { return Convert(str
, str
); }
96 wxString
Convert(const wxString
& input
) const;
99 bool Convert(const char* input
, wchar_t* output
) const;
100 bool Convert(const wchar_t* input
, char* output
) const;
101 bool Convert(const wchar_t* input
, wchar_t* output
) const;
102 bool Convert(wchar_t* str
) const { return Convert(str
, str
); }
104 // Return equivalent(s) for given font that are used
105 // under given platform. wxPLATFORM_CURRENT means the plaform
106 // this binary was compiled for
109 // current platform enc returned value
110 // -----------------------------------------------------
111 // unix CP1250 {ISO8859_2}
113 // windows ISO8859_2 {CP1250}
115 // Equivalence is defined in terms of convertibility:
116 // 2 encodings are equivalent if you can convert text between
117 // then without loosing information (it may - and will - happen
118 // that you loose special chars like quotation marks or em-dashes
119 // but you shouldn't loose any diacritics and language-specific
120 // characters when converting between equivalent encodings).
122 // Convert() method is not limited to converting between
123 // equivalent encodings, it can convert between arbitrary
126 // Remember that this function does _NOT_ check for presence of
127 // fonts in system. It only tells you what are most suitable
128 // encodings. (It usually returns only one encoding)
130 // Note that argument enc itself may be present in returned array!
131 // (so that you can -- as a side effect -- detect whether the
132 // encoding is native for this platform or not)
133 static wxFontEncodingArray
GetPlatformEquivalents(wxFontEncoding enc
, int platform
= wxPLATFORM_CURRENT
);
135 // Similar to GetPlatformEquivalent, but this one will return ALL
136 // equivalent encodings, regardless the platform, including itself.
137 static wxFontEncodingArray
GetAllEquivalents(wxFontEncoding enc
);
139 // Return true if [any text in] one multibyte encoding can be
140 // converted to another one losslessly.
142 // Do not call this with wxFONTENCODING_UNICODE, it doesn't make
143 // sense (always works in one sense and always depends on the text
144 // to convert in the other)
145 static bool CanConvert(wxFontEncoding encIn
, wxFontEncoding encOut
)
147 return GetAllEquivalents(encIn
).Index(encOut
) != wxNOT_FOUND
;
157 bool m_UnicodeInput
, m_UnicodeOutput
;
160 DECLARE_NO_COPY_CLASS(wxEncodingConverter
)
163 #endif // _WX_ENCCONV_H_