2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/text/character-set-conversion.h>
32 const static uint8_t U1 = 1u;
33 const static uint8_t U2 = 2u;
34 const static uint8_t U3 = 3u;
35 const static uint8_t U4 = 4u;
36 const static uint8_t U5 = 5u;
37 const static uint8_t U6 = 6u;
38 const static uint8_t U0 = 0u;
39 const static uint8_t UTF8_LENGTH[256] = {
40 U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
41 U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
42 U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
43 U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
44 U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
45 U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
46 U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
47 U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
48 U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, // lead byte = 0xxx xxxx (U+0000 - U+007F + some extended ascii characters)
49 U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
50 U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
51 U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
52 U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
53 U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
54 U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
55 U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
56 U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
57 U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
58 U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
61 U2, U2, U2, U2, U2, U2, U2, U2, U2, U2, //
62 U2, U2, U2, U2, U2, U2, U2, U2, U2, U2, // lead byte = 110x xxxx (U+0080 - U+07FF)
63 U2, U2, U2, U2, U2, U2, U2, U2, U2, U2, //
66 U3, U3, U3, U3, U3, U3, U3, U3, U3, U3, // lead byte = 1110 xxxx (U+0800 - U+FFFF)
67 U3, U3, U3, U3, U3, U3, //
69 U4, U4, U4, U4, U4, U4, U4, U4, // lead byte = 1111 0xxx (U+10000 - U+1FFFFF)
71 U5, U5, U5, U5, // lead byte = 1111 10xx (U+200000 - U+3FFFFFF)
73 U6, U6, // lead byte = 1111 110x (U+4000000 - U+7FFFFFFF)
78 const uint8_t CR = 0xd;
79 const uint8_t LF = 0xa;
82 uint8_t GetUtf8Length( uint8_t utf8LeadByte )
84 return UTF8_LENGTH[utf8LeadByte];
87 uint32_t GetNumberOfUtf8Characters( const uint8_t* const utf8, uint32_t length )
89 uint32_t numberOfCharacters = 0u;
91 const uint8_t* begin = utf8;
92 const uint8_t* end = utf8 + length;
94 for( ; begin < end ; begin += UTF8_LENGTH[*begin], ++numberOfCharacters );
96 return numberOfCharacters;
99 uint32_t GetNumberOfUtf8Bytes( const uint32_t* const utf32, uint32_t numberOfCharacters )
101 uint32_t numberOfBytes = 0u;
103 const uint32_t* begin = utf32;
104 const uint32_t* end = utf32 + numberOfCharacters;
106 for( ; begin < end; ++begin )
108 const uint32_t code = *begin;
114 else if( code < 0x800u )
118 else if( code < 0x10000u )
122 else if( code < 0x200000u )
126 else if( code < 0x4000000u )
130 else if( code < 0x80000000u )
136 return numberOfBytes;
139 uint32_t Utf8ToUtf32( const uint8_t* const utf8, uint32_t length, uint32_t* utf32 )
141 uint32_t numberOfCharacters = 0u;
143 const uint8_t* begin = utf8;
144 const uint8_t* end = utf8 + length;
146 for( ; begin < end ; ++numberOfCharacters )
148 const uint8_t leadByte = *begin;
150 switch( UTF8_LENGTH[leadByte] )
156 // Replace CR+LF or CR by LF
159 // Look ahead if the next one is a LF.
179 uint32_t& code = *utf32++;
180 code = leadByte & 0x1fu;
183 code |= *begin++ & 0x3fu;
189 uint32_t& code = *utf32++;
190 code = leadByte & 0x0fu;
193 code |= *begin++ & 0x3fu;
195 code |= *begin++ & 0x3fu;
201 uint32_t& code = *utf32++;
202 code = leadByte & 0x07u;
205 code |= *begin++ & 0x3fu;
207 code |= *begin++ & 0x3fu;
209 code |= *begin++ & 0x3fu;
215 uint32_t& code = *utf32++;
216 code = leadByte & 0x03u;
219 code |= *begin++ & 0x3fu;
221 code |= *begin++ & 0x3fu;
223 code |= *begin++ & 0x3fu;
225 code |= *begin++ & 0x3fu;
231 uint32_t& code = *utf32++;
232 code = leadByte & 0x01u;
235 code |= *begin++ & 0x3fu;
237 code |= *begin++ & 0x3fu;
239 code |= *begin++ & 0x3fu;
241 code |= *begin++ & 0x3fu;
243 code |= *begin++ & 0x3fu;
247 case U0: // Invalid case
250 *utf32++ = 0x20; // Use white space
256 return numberOfCharacters;
259 uint32_t Utf32ToUtf8( const uint32_t* const utf32, uint32_t numberOfCharacters, uint8_t* utf8 )
261 const uint32_t* begin = utf32;
262 const uint32_t* end = utf32 + numberOfCharacters;
264 uint8_t* utf8Begin = utf8;
266 for( ; begin < end; ++begin )
268 const uint32_t code = *begin;
274 else if( code < 0x800u )
276 *utf8++ = static_cast<uint8_t>( code >> 6u ) | 0xc0u; // lead byte for 2 byte sequence
277 *utf8++ = static_cast<uint8_t>( code & 0x3f ) | 0x80u; // continuation byte
279 else if( code < 0x10000u )
281 *utf8++ = static_cast<uint8_t>( code >> 12u ) | 0xe0u; // lead byte for 3 byte sequence
282 *utf8++ = static_cast<uint8_t>( ( code >> 6u ) & 0x3f ) | 0x80u; // continuation byte
283 *utf8++ = static_cast<uint8_t>( code & 0x3f ) | 0x80u; // continuation byte
285 else if( code < 0x200000u )
287 *utf8++ = static_cast<uint8_t>( code >> 18u ) | 0xf0u; // lead byte for 4 byte sequence
288 *utf8++ = static_cast<uint8_t>( ( code >> 12u ) & 0x3f ) | 0x80u; // continuation byte
289 *utf8++ = static_cast<uint8_t>( ( code >> 6u ) & 0x3f ) | 0x80u; // continuation byte
290 *utf8++ = static_cast<uint8_t>( code & 0x3f ) | 0x80u; // continuation byte
292 else if( code < 0x4000000u )
294 *utf8++ = static_cast<uint8_t>( code >> 24u ) | 0xf8u; // lead byte for 5 byte sequence
295 *utf8++ = static_cast<uint8_t>( ( code >> 18u ) & 0x3f ) | 0x80u; // continuation byte
296 *utf8++ = static_cast<uint8_t>( ( code >> 12u ) & 0x3f ) | 0x80u; // continuation byte
297 *utf8++ = static_cast<uint8_t>( ( code >> 6u ) & 0x3f ) | 0x80u; // continuation byte
298 *utf8++ = static_cast<uint8_t>( code & 0x3f ) | 0x80u; // continuation byte
300 else if( code < 0x80000000u )
302 *utf8++ = static_cast<uint8_t>( code >> 30u ) | 0xfcu; // lead byte for 6 byte sequence
303 *utf8++ = static_cast<uint8_t>( ( code >> 24u ) & 0x3f ) | 0x80u; // continuation byte
304 *utf8++ = static_cast<uint8_t>( ( code >> 18u ) & 0x3f ) | 0x80u; // continuation byte
305 *utf8++ = static_cast<uint8_t>( ( code >> 12u ) & 0x3f ) | 0x80u; // continuation byte
306 *utf8++ = static_cast<uint8_t>( ( code >> 6u ) & 0x3f ) | 0x80u; // continuation byte
307 *utf8++ = static_cast<uint8_t>( code & 0x3f ) | 0x80u; // continuation byte
311 return utf8 - utf8Begin;
314 void Utf32ToUtf8( const uint32_t* const utf32, uint32_t numberOfCharacters, std::string& utf8 )
318 uint32_t numberOfBytes = GetNumberOfUtf8Bytes( &utf32[0], numberOfCharacters );
319 utf8.resize( numberOfBytes );
321 // This is a bit horrible but std::string returns a (signed) char*
322 Utf32ToUtf8( utf32, numberOfCharacters, reinterpret_cast<uint8_t*>(&utf8[0]) );
327 } // namespace Toolkit