1 ////////////////////////////////////////////////////////////
\r
3 // SFML - Simple and Fast Multimedia Library
\r
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
\r
6 // This software is provided 'as-is', without any express or implied warranty.
\r
7 // In no event will the authors be held liable for any damages arising from the use of this software.
\r
9 // Permission is granted to anyone to use this software for any purpose,
\r
10 // including commercial applications, and to alter it and redistribute it freely,
\r
11 // subject to the following restrictions:
\r
13 // 1. The origin of this software must not be misrepresented;
\r
14 // you must not claim that you wrote the original software.
\r
15 // If you use this software in a product, an acknowledgment
\r
16 // in the product documentation would be appreciated but is not required.
\r
18 // 2. Altered source versions must be plainly marked as such,
\r
19 // and must not be misrepresented as being the original software.
\r
21 // 3. This notice may not be removed or altered from any source distribution.
\r
23 ////////////////////////////////////////////////////////////
\r
25 #ifndef SFML_UNICODE_HPP
\r
26 #define SFML_UNICODE_HPP
\r
28 ////////////////////////////////////////////////////////////
\r
30 ////////////////////////////////////////////////////////////
\r
31 #include <SFML/Config.hpp>
\r
40 ////////////////////////////////////////////////////////////
\r
41 /// Provides utility functions to convert from and to
\r
42 /// any unicode and ASCII encoding
\r
43 ////////////////////////////////////////////////////////////
\r
44 class SFML_API Unicode
\r
48 ////////////////////////////////////////////////////////////
\r
49 /// Define a string type for each encoding
\r
50 /// Warning : in UTF8 and UTF16 strings, one element doesn't
\r
51 /// necessarily maps to one character ; only an UTF32 element
\r
52 /// is wide enough to hold all possible unicode values
\r
53 ////////////////////////////////////////////////////////////
\r
54 typedef std::basic_string<Uint8> UTF8String;
\r
55 typedef std::basic_string<Uint16> UTF16String;
\r
56 typedef std::basic_string<Uint32> UTF32String;
\r
58 ////////////////////////////////////////////////////////////
\r
59 /// This class is an abstract definition of a unicode text,
\r
60 /// it can be converted from and to any kind of string
\r
62 ////////////////////////////////////////////////////////////
\r
67 ////////////////////////////////////////////////////////////
\r
68 /// Default constructor (empty text)
\r
70 ////////////////////////////////////////////////////////////
\r
73 ////////////////////////////////////////////////////////////
\r
74 /// Construct the unicode text from any type of string
\r
76 /// \param Str : String to convert
\r
78 ////////////////////////////////////////////////////////////
\r
79 Text(const char* Str);
\r
80 Text(const wchar_t* Str);
\r
81 Text(const Uint8* Str);
\r
82 Text(const Uint16* Str);
\r
83 Text(const Uint32* Str);
\r
84 Text(const std::string& Str);
\r
85 Text(const std::wstring& Str);
\r
86 Text(const Unicode::UTF8String& Str);
\r
87 Text(const Unicode::UTF16String& Str);
\r
88 Text(const Unicode::UTF32String& Str);
\r
90 ////////////////////////////////////////////////////////////
\r
91 /// Operator to cast the text to any type of string
\r
93 /// \return Converted string
\r
95 ////////////////////////////////////////////////////////////
\r
96 operator std::string () const;
\r
97 operator std::wstring () const;
\r
98 operator Unicode::UTF8String () const;
\r
99 operator Unicode::UTF16String () const;
\r
100 operator const Unicode::UTF32String&() const;
\r
104 ////////////////////////////////////////////////////////////
\r
106 ////////////////////////////////////////////////////////////
\r
107 sf::Unicode::UTF32String myUTF32String; ///< UTF-32 unicode text
\r
110 ////////////////////////////////////////////////////////////
\r
111 /// Generic function to convert an UTF-32 characters range
\r
112 /// to an ANSI characters range, using the given locale
\r
114 /// \param Begin : Iterator pointing to the beginning of the input sequence
\r
115 /// \param End : Iterator pointing to the end of the input sequence
\r
116 /// \param Output : Iterator pointing to the beginning of the output sequence
\r
117 /// \param Replacement : Replacement character for characters not convertible to output encoding ('?' by default -- use 0 to use no replacement character)
\r
118 /// \param Locale : Locale to use for conversion (uses the current one by default)
\r
120 /// \return Iterator to the end of the output sequence which has been written
\r
122 ////////////////////////////////////////////////////////////
\r
123 template <typename In, typename Out>
\r
124 static Out UTF32ToANSI(In Begin, In End, Out Output, char Replacement = '?', const std::locale& Locale = GetDefaultLocale());
\r
126 ////////////////////////////////////////////////////////////
\r
127 /// Generic function to convert an ANSI characters range
\r
128 /// to an UTF-32 characters range, using the given locale
\r
130 /// \param Begin : Iterator pointing to the beginning of the input sequence
\r
131 /// \param End : Iterator pointing to the end of the input sequence
\r
132 /// \param Output : Iterator pointing to the beginning of the output sequence
\r
133 /// \param Locale : Locale to use for conversion (uses the current one by default)
\r
135 /// \return Iterator to the end of the output sequence which has been written
\r
137 ////////////////////////////////////////////////////////////
\r
138 template <typename In, typename Out>
\r
139 static Out ANSIToUTF32(In Begin, In End, Out Output, const std::locale& Locale = GetDefaultLocale());
\r
141 ////////////////////////////////////////////////////////////
\r
142 /// Generic function to convert an UTF-8 characters range
\r
143 /// to an UTF-16 characters range, using the given locale
\r
145 /// \param Begin : Iterator pointing to the beginning of the input sequence
\r
146 /// \param End : Iterator pointing to the end of the input sequence
\r
147 /// \param Output : Iterator pointing to the beginning of the output sequence
\r
148 /// \param Replacement : Replacement character for characters not convertible to output encoding ('?' by default -- use 0 to use no replacement character)
\r
150 /// \return Iterator to the end of the output sequence which has been written
\r
152 ////////////////////////////////////////////////////////////
\r
153 template <typename In, typename Out>
\r
154 static Out UTF8ToUTF16(In Begin, In End, Out Output, Uint16 Replacement = '?');
\r
156 ////////////////////////////////////////////////////////////
\r
157 /// Generic function to convert an UTF-8 characters range
\r
158 /// to an UTF-32 characters range, using the given locale
\r
160 /// \param Begin : Iterator pointing to the beginning of the input sequence
\r
161 /// \param End : Iterator pointing to the end of the input sequence
\r
162 /// \param Output : Iterator pointing to the beginning of the output sequence
\r
163 /// \param Replacement : Replacement character for characters not convertible to output encoding ('?' by default -- use 0 to use no replacement character)
\r
165 /// \return Iterator to the end of the output sequence which has been written
\r
167 ////////////////////////////////////////////////////////////
\r
168 template <typename In, typename Out>
\r
169 static Out UTF8ToUTF32(In Begin, In End, Out Output, Uint32 Replacement = '?');
\r
171 ////////////////////////////////////////////////////////////
\r
172 /// Generic function to convert an UTF-16 characters range
\r
173 /// to an UTF-8 characters range, using the given locale
\r
175 /// \param Begin : Iterator pointing to the beginning of the input sequence
\r
176 /// \param End : Iterator pointing to the end of the input sequence
\r
177 /// \param Output : Iterator pointing to the beginning of the output sequence
\r
178 /// \param Replacement : Replacement character for characters not convertible to output encoding ('?' by default -- use 0 to use no replacement character)
\r
180 /// \return Iterator to the end of the output sequence which has been written
\r
182 ////////////////////////////////////////////////////////////
\r
183 template <typename In, typename Out>
\r
184 static Out UTF16ToUTF8(In Begin, In End, Out Output, Uint8 Replacement = '?');
\r
186 ////////////////////////////////////////////////////////////
\r
187 /// Generic function to convert an UTF-16 characters range
\r
188 /// to an UTF-32 characters range, using the given locale
\r
190 /// \param Begin : Iterator pointing to the beginning of the input sequence
\r
191 /// \param End : Iterator pointing to the end of the input sequence
\r
192 /// \param Output : Iterator pointing to the beginning of the output sequence
\r
193 /// \param Replacement : Replacement character for characters not convertible to output encoding ('?' by default -- use 0 to use no replacement character)
\r
195 /// \return Iterator to the end of the output sequence which has been written
\r
197 ////////////////////////////////////////////////////////////
\r
198 template <typename In, typename Out>
\r
199 static Out UTF16ToUTF32(In Begin, In End, Out Output, Uint32 Replacement = '?');
\r
201 ////////////////////////////////////////////////////////////
\r
202 /// Generic function to convert an UTF-32 characters range
\r
203 /// to an UTF-8 characters range, using the given locale
\r
205 /// \param Begin : Iterator pointing to the beginning of the input sequence
\r
206 /// \param End : Iterator pointing to the end of the input sequence
\r
207 /// \param Output : Iterator pointing to the beginning of the output sequence
\r
208 /// \param Replacement : Replacement character for characters not convertible to output encoding ('?' by default -- use 0 to use no replacement character)
\r
210 /// \return Iterator to the end of the output sequence which has been written
\r
212 ////////////////////////////////////////////////////////////
\r
213 template <typename In, typename Out>
\r
214 static Out UTF32ToUTF8(In Begin, In End, Out Output, Uint8 Replacement = '?');
\r
216 ////////////////////////////////////////////////////////////
\r
217 /// Generic function to convert an UTF-32 characters range
\r
218 /// to an UTF-16 characters range, using the given locale
\r
220 /// \param Begin : Iterator pointing to the beginning of the input sequence
\r
221 /// \param End : Iterator pointing to the end of the input sequence
\r
222 /// \param Output : Iterator pointing to the beginning of the output sequence
\r
223 /// \param Replacement : Replacement character for characters not convertible to output encoding ('?' by default -- use 0 to use no replacement character)
\r
225 /// \return Iterator to the end of the output sequence which has been written
\r
227 ////////////////////////////////////////////////////////////
\r
228 template <typename In, typename Out>
\r
229 static Out UTF32ToUTF16(In Begin, In End, Out Output, Uint16 Replacement = '?');
\r
231 ////////////////////////////////////////////////////////////
\r
232 /// Get the number of characters composing an UTF-8 string
\r
234 /// \param Begin : Iterator pointing to the beginning of the input sequence
\r
235 /// \param End : Iterator pointing to the end of the input sequence
\r
237 /// \return Count of the characters in the string
\r
239 ////////////////////////////////////////////////////////////
\r
240 template <typename In>
\r
241 static std::size_t GetUTF8Length(In Begin, In End);
\r
243 ////////////////////////////////////////////////////////////
\r
244 /// Get the number of characters composing an UTF-16 string
\r
246 /// \param Begin : Iterator pointing to the beginning of the input sequence
\r
247 /// \param End : Iterator pointing to the end of the input sequence
\r
249 /// \return Count of the characters in the string
\r
251 ////////////////////////////////////////////////////////////
\r
252 template <typename In>
\r
253 static std::size_t GetUTF16Length(In Begin, In End);
\r
255 ////////////////////////////////////////////////////////////
\r
256 /// Get the number of characters composing an UTF-32 string
\r
258 /// \param Begin : Iterator pointing to the beginning of the input sequence
\r
259 /// \param End : Iterator pointing to the end of the input sequence
\r
261 /// \return Count of the characters in the string
\r
263 ////////////////////////////////////////////////////////////
\r
264 template <typename In>
\r
265 static std::size_t GetUTF32Length(In Begin, In End);
\r
269 ////////////////////////////////////////////////////////////
\r
270 /// Get the default system locale
\r
272 /// \return Reference to the default system locale
\r
274 ////////////////////////////////////////////////////////////
\r
275 static const std::locale& GetDefaultLocale();
\r
277 ////////////////////////////////////////////////////////////
\r
278 // Static member data
\r
279 ////////////////////////////////////////////////////////////
\r
280 static const int UTF8TrailingBytes[256]; ///< Lookup table to find the length of an UTF-8 sequence
\r
281 static const Uint32 UTF8Offsets[6]; ///< Magic values to subtract during UTF-8 conversions
\r
282 static const Uint8 UTF8FirstBytes[7]; ///< First bytes for UTF-8 sequences
\r
285 #include <SFML/System/Unicode.inl>
\r
290 #endif // SFML_UNICODE_HPP
\r