]> git.sesse.net Git - casparcg/blob - dependencies64/sfml/include/SFML/System/Unicode.hpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / dependencies64 / sfml / include / SFML / System / Unicode.hpp
1 ////////////////////////////////////////////////////////////\r
2 //\r
3 // SFML - Simple and Fast Multimedia Library\r
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)\r
5 //\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
8 //\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
12 //\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
17 //\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
20 //\r
21 // 3. This notice may not be removed or altered from any source distribution.\r
22 //\r
23 ////////////////////////////////////////////////////////////\r
24 \r
25 #ifndef SFML_UNICODE_HPP\r
26 #define SFML_UNICODE_HPP\r
27 \r
28 ////////////////////////////////////////////////////////////\r
29 // Headers\r
30 ////////////////////////////////////////////////////////////\r
31 #include <SFML/Config.hpp>\r
32 #include <iterator>\r
33 #include <locale>\r
34 #include <string>\r
35 #include <stdlib.h>\r
36 \r
37 \r
38 namespace sf\r
39 {\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
45 {\r
46 public :\r
47 \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
57 \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
61     /// and encoding\r
62     ////////////////////////////////////////////////////////////\r
63     class SFML_API Text\r
64     {\r
65     public :\r
66 \r
67         ////////////////////////////////////////////////////////////\r
68         /// Default constructor (empty text)\r
69         ///\r
70         ////////////////////////////////////////////////////////////\r
71         Text();\r
72 \r
73         ////////////////////////////////////////////////////////////\r
74         /// Construct the unicode text from any type of string\r
75         ///\r
76         /// \param Str : String to convert\r
77         ///\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
89 \r
90         ////////////////////////////////////////////////////////////\r
91         /// Operator to cast the text to any type of string\r
92         ///\r
93         /// \return Converted string\r
94         ///\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
101 \r
102     private :\r
103 \r
104         ////////////////////////////////////////////////////////////\r
105         // Data member\r
106         ////////////////////////////////////////////////////////////\r
107         sf::Unicode::UTF32String myUTF32String; ///< UTF-32 unicode text\r
108     };\r
109 \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
113     ///\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
119     ///\r
120     /// \return Iterator to the end of the output sequence which has been written\r
121     ///\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
125 \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
129     ///\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
134     ///\r
135     /// \return Iterator to the end of the output sequence which has been written\r
136     ///\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
140 \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
144     ///\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
149     ///\r
150     /// \return Iterator to the end of the output sequence which has been written\r
151     ///\r
152     ////////////////////////////////////////////////////////////\r
153     template <typename In, typename Out>\r
154     static Out UTF8ToUTF16(In Begin, In End, Out Output, Uint16 Replacement = '?');\r
155 \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
159     ///\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
164     ///\r
165     /// \return Iterator to the end of the output sequence which has been written\r
166     ///\r
167     ////////////////////////////////////////////////////////////\r
168     template <typename In, typename Out>\r
169     static Out UTF8ToUTF32(In Begin, In End, Out Output, Uint32 Replacement = '?');\r
170 \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
174     ///\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
179     ///\r
180     /// \return Iterator to the end of the output sequence which has been written\r
181     ///\r
182     ////////////////////////////////////////////////////////////\r
183     template <typename In, typename Out>\r
184     static Out UTF16ToUTF8(In Begin, In End, Out Output, Uint8 Replacement = '?');\r
185 \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
189     ///\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
194     ///\r
195     /// \return Iterator to the end of the output sequence which has been written\r
196     ///\r
197     ////////////////////////////////////////////////////////////\r
198     template <typename In, typename Out>\r
199     static Out UTF16ToUTF32(In Begin, In End, Out Output, Uint32 Replacement = '?');\r
200 \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
204     ///\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
209     ///\r
210     /// \return Iterator to the end of the output sequence which has been written\r
211     ///\r
212     ////////////////////////////////////////////////////////////\r
213     template <typename In, typename Out>\r
214     static Out UTF32ToUTF8(In Begin, In End, Out Output, Uint8 Replacement = '?');\r
215 \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
219     ///\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
224     ///\r
225     /// \return Iterator to the end of the output sequence which has been written\r
226     ///\r
227     ////////////////////////////////////////////////////////////\r
228     template <typename In, typename Out>\r
229     static Out UTF32ToUTF16(In Begin, In End, Out Output, Uint16 Replacement = '?');\r
230 \r
231     ////////////////////////////////////////////////////////////\r
232     /// Get the number of characters composing an UTF-8 string\r
233     ///\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
236     ///\r
237     /// \return Count of the characters in the string\r
238     ///\r
239     ////////////////////////////////////////////////////////////\r
240     template <typename In>\r
241     static std::size_t GetUTF8Length(In Begin, In End);\r
242 \r
243     ////////////////////////////////////////////////////////////\r
244     /// Get the number of characters composing an UTF-16 string\r
245     ///\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
248     ///\r
249     /// \return Count of the characters in the string\r
250     ///\r
251     ////////////////////////////////////////////////////////////\r
252     template <typename In>\r
253     static std::size_t GetUTF16Length(In Begin, In End);\r
254 \r
255     ////////////////////////////////////////////////////////////\r
256     /// Get the number of characters composing an UTF-32 string\r
257     ///\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
260     ///\r
261     /// \return Count of the characters in the string\r
262     ///\r
263     ////////////////////////////////////////////////////////////\r
264     template <typename In>\r
265     static std::size_t GetUTF32Length(In Begin, In End);\r
266 \r
267 private :\r
268 \r
269     ////////////////////////////////////////////////////////////\r
270     /// Get the default system locale\r
271     ///\r
272     /// \return Reference to the default system locale\r
273     ///\r
274     ////////////////////////////////////////////////////////////\r
275     static const std::locale& GetDefaultLocale();\r
276 \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
283 };\r
284 \r
285 #include <SFML/System/Unicode.inl>\r
286 \r
287 } // namespace sf\r
288 \r
289 \r
290 #endif // SFML_UNICODE_HPP\r