Revert "License conversion from Flora to Apache 2.0"
[platform/core/uifw/dali-core.git] / dali / internal / event / text / utf8-impl.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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
7 //
8 //     http://floralicense.org/license/
9 //
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.
15 //
16
17 // CLASS HEADER
18 #include <dali/internal/event/text/utf8-impl.h>
19
20 // INTERNAL INCLUDES
21 #include <dali/integration-api/debug.h>
22
23 namespace Dali
24 {
25
26 namespace Internal
27 {
28
29 size_t UTF8SequenceLength(const unsigned char leadByte)
30 {
31   size_t length = 0;
32
33   if ((leadByte & 0x80) == 0 )          //ASCII character (lead bit zero)
34   {
35     length = 1;
36   }
37   else if (( leadByte & 0xe0 ) == 0xc0 ) //110x xxxx
38   {
39     length = 2;
40   }
41   else if (( leadByte & 0xf0 ) == 0xe0 ) //1110 xxxx
42   {
43     length = 3;
44   }
45   else if (( leadByte & 0xf8 ) == 0xf0 ) //1111 0xxx
46   {
47     length = 4;
48   }
49   else
50   {
51     DALI_LOG_WARNING("Unrecognized lead byte  %c\n", leadByte);
52   }
53
54   return length;
55 }
56
57 uint32_t UTF8Read(const unsigned char* utf8Data, const size_t sequenceLength)
58 {
59   uint32_t code = 0;
60
61   if (sequenceLength == 1)
62   {
63     code = *utf8Data;
64   }
65   else if (sequenceLength == 2)
66   {
67     code = *utf8Data++ & 0x1f;
68     code <<= 6;
69     code |= *utf8Data  & 0x3f;
70   }
71   else if (sequenceLength == 3)
72   {
73     code =  *utf8Data++ & 0x0f;
74     code <<= 6;
75     code |= *utf8Data++ & 0x3f;
76     code <<= 6;
77     code |= *utf8Data   & 0x3f;
78   }
79   else if (sequenceLength == 4)
80   {
81     code =  *utf8Data++ & 0x07;
82     code <<= 6;
83     code |= *utf8Data++ & 0x3f;
84     code <<= 6;
85     code |= *utf8Data++ & 0x3f;
86     code <<= 6;
87     code |= *utf8Data   & 0x3f;
88   }
89
90   return code;
91 }
92
93 size_t UTF8Write(const uint32_t code, unsigned char* utf8Data)
94 {
95   size_t sequenceLength = 0;
96   if (code < 0x80u)
97   {
98     *utf8Data = code;
99     sequenceLength = 1;
100   }
101   else if (code < 0x800u)
102   {
103     *utf8Data++ = static_cast<uint8_t>( code >> 6)          | 0xc0; // lead byte for 2 byte sequence
104     *utf8Data   = static_cast<uint8_t>( code        & 0x3f) | 0x80; // continuation byte
105     sequenceLength = 2;
106   }
107   else if (code < 0x10000u)
108   {
109     *utf8Data++ = static_cast<uint8_t>( code >> 12)         | 0xe0; // lead byte for 2 byte sequence
110     *utf8Data++ = static_cast<uint8_t>((code >> 6)  & 0x3f) | 0x80; // continuation byte
111     *utf8Data   = static_cast<uint8_t>( code        & 0x3f) | 0x80; // continuation byte
112     sequenceLength = 3;
113   }
114   else if (code < 0x200000u)
115   {
116     *utf8Data++ = static_cast<uint8_t>( code >> 18)         | 0xf0; // lead byte for 2 byte sequence
117     *utf8Data++ = static_cast<uint8_t>((code >> 12) & 0x3f) | 0x80; // continuation byte
118     *utf8Data++ = static_cast<uint8_t>((code >> 6)  & 0x3f) | 0x80; // continuation byte
119     *utf8Data   = static_cast<uint8_t>( code        & 0x3f) | 0x80; // continuation byte
120     sequenceLength = 4;
121   }
122
123   return sequenceLength;
124 }
125
126 size_t UTF8Tokenize(const unsigned char* utf8Data, const size_t utf8DataLength, TextArray& tokens)
127 {
128   size_t dataLength = utf8DataLength;
129
130   while (dataLength)
131   {
132     uint32_t code;
133     size_t sequenceLength = UTF8SequenceLength(*utf8Data);
134
135     if (!sequenceLength)
136     {
137       break;
138     }
139
140     if (sequenceLength > dataLength)
141     {
142       // utf8 data error
143       break;
144     }
145
146     code = UTF8Read(utf8Data, sequenceLength);
147     tokens.push_back(code);
148
149     utf8Data += sequenceLength;
150     dataLength -= sequenceLength;
151   }
152   return tokens.size();
153 }
154
155 } // namespace Internal
156
157 } // namespace Dali