/src/wxwidgets/include/wx/encconv.h
Line | Count | Source |
1 | | ///////////////////////////////////////////////////////////////////////////// |
2 | | // Name: wx/encconv.h |
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 | | #ifndef _WX_ENCCONV_H_ |
11 | | #define _WX_ENCCONV_H_ |
12 | | |
13 | | #include "wx/defs.h" |
14 | | |
15 | | #include "wx/object.h" |
16 | | #include "wx/fontenc.h" |
17 | | #include "wx/dynarray.h" |
18 | | |
19 | | // ---------------------------------------------------------------------------- |
20 | | // constants |
21 | | // ---------------------------------------------------------------------------- |
22 | | |
23 | | enum |
24 | | { |
25 | | wxCONVERT_STRICT, |
26 | | wxCONVERT_SUBSTITUTE |
27 | | }; |
28 | | |
29 | | |
30 | | enum |
31 | | { |
32 | | wxPLATFORM_CURRENT = -1, |
33 | | |
34 | | wxPLATFORM_UNIX = 0, |
35 | | wxPLATFORM_WINDOWS, |
36 | | wxPLATFORM_MAC |
37 | | }; |
38 | | |
39 | | // ---------------------------------------------------------------------------- |
40 | | // types |
41 | | // ---------------------------------------------------------------------------- |
42 | | |
43 | | WX_DEFINE_ARRAY_INT(wxFontEncoding, wxFontEncodingArray); |
44 | | |
45 | | //-------------------------------------------------------------------------------- |
46 | | // wxEncodingConverter |
47 | | // This class is capable of converting strings between any two |
48 | | // 8bit encodings/charsets. It can also convert from/to Unicode |
49 | | //-------------------------------------------------------------------------------- |
50 | | |
51 | | class WXDLLIMPEXP_BASE wxEncodingConverter : public wxObject |
52 | | { |
53 | | public: |
54 | | |
55 | | wxEncodingConverter(); |
56 | 0 | virtual ~wxEncodingConverter() { delete[] m_Table; } |
57 | | |
58 | | // Initialize conversion. Both output or input encoding may |
59 | | // be wxFONTENCODING_UNICODE. |
60 | | // |
61 | | // All subsequent calls to Convert() will interpret it's argument |
62 | | // as a string in input_enc encoding and will output string in |
63 | | // output_enc encoding. |
64 | | // |
65 | | // You must call this method before calling Convert. You may call |
66 | | // it more than once in order to switch to another conversion |
67 | | // |
68 | | // Method affects behaviour of Convert() in case input character |
69 | | // cannot be converted because it does not exist in output encoding: |
70 | | // wxCONVERT_STRICT -- |
71 | | // follow behaviour of GNU Recode - just copy unconvertable |
72 | | // characters to output and don't change them (it's integer |
73 | | // value will stay the same) |
74 | | // wxCONVERT_SUBSTITUTE -- |
75 | | // try some (lossy) substitutions - e.g. replace |
76 | | // unconvertable latin capitals with acute by ordinary |
77 | | // capitals, replace en-dash or em-dash by '-' etc. |
78 | | // both modes guarantee that output string will have same length |
79 | | // as input string |
80 | | // |
81 | | // Returns false if given conversion is impossible, true otherwise |
82 | | // (conversion may be impossible either if you try to convert |
83 | | // to Unicode with non-Unicode build of wxWidgets or if input |
84 | | // or output encoding is not supported.) |
85 | | bool Init(wxFontEncoding input_enc, wxFontEncoding output_enc, int method = wxCONVERT_STRICT); |
86 | | |
87 | | // Convert input string according to settings passed to Init. |
88 | | // Note that you must call Init before using Convert! |
89 | | bool Convert(const char* input, char* output) const; |
90 | 0 | bool Convert(char* str) const { return Convert(str, str); } |
91 | | wxString Convert(const wxString& input) const; |
92 | | |
93 | | bool Convert(const char* input, wchar_t* output) const; |
94 | | bool Convert(const wchar_t* input, char* output) const; |
95 | | bool Convert(const wchar_t* input, wchar_t* output) const; |
96 | 0 | bool Convert(wchar_t* str) const { return Convert(str, str); } |
97 | | |
98 | | // Return equivalent(s) for given font that are used |
99 | | // under given platform. wxPLATFORM_CURRENT means the platform |
100 | | // this binary was compiled for |
101 | | // |
102 | | // Examples: |
103 | | // current platform enc returned value |
104 | | // ----------------------------------------------------- |
105 | | // unix CP1250 {ISO8859_2} |
106 | | // unix ISO8859_2 {} |
107 | | // windows ISO8859_2 {CP1250} |
108 | | // |
109 | | // Equivalence is defined in terms of convertibility: |
110 | | // 2 encodings are equivalent if you can convert text between |
111 | | // then without losing information (it may - and will - happen |
112 | | // that you lose special chars like quotation marks or em-dashes |
113 | | // but you shouldn't lose any diacritics and language-specific |
114 | | // characters when converting between equivalent encodings). |
115 | | // |
116 | | // Convert() method is not limited to converting between |
117 | | // equivalent encodings, it can convert between arbitrary |
118 | | // two encodings! |
119 | | // |
120 | | // Remember that this function does _NOT_ check for presence of |
121 | | // fonts in system. It only tells you what are most suitable |
122 | | // encodings. (It usually returns only one encoding) |
123 | | // |
124 | | // Note that argument enc itself may be present in returned array! |
125 | | // (so that you can -- as a side effect -- detect whether the |
126 | | // encoding is native for this platform or not) |
127 | | static wxFontEncodingArray GetPlatformEquivalents(wxFontEncoding enc, int platform = wxPLATFORM_CURRENT); |
128 | | |
129 | | // Similar to GetPlatformEquivalent, but this one will return ALL |
130 | | // equivalent encodings, regardless the platform, including itself. |
131 | | static wxFontEncodingArray GetAllEquivalents(wxFontEncoding enc); |
132 | | |
133 | | // Return true if [any text in] one multibyte encoding can be |
134 | | // converted to another one losslessly. |
135 | | // |
136 | | // Do not call this with wxFONTENCODING_UNICODE, it doesn't make |
137 | | // sense (always works in one sense and always depends on the text |
138 | | // to convert in the other) |
139 | | static bool CanConvert(wxFontEncoding encIn, wxFontEncoding encOut) |
140 | 0 | { |
141 | 0 | return GetAllEquivalents(encIn).Index(encOut) != wxNOT_FOUND; |
142 | 0 | } |
143 | | |
144 | | private: |
145 | | wchar_t *m_Table; |
146 | | bool m_UnicodeInput, m_UnicodeOutput; |
147 | | bool m_JustCopy; |
148 | | |
149 | | wxDECLARE_NO_COPY_CLASS(wxEncodingConverter); |
150 | | }; |
151 | | |
152 | | #endif // _WX_ENCCONV_H_ |