Removed some redundant methods from TextController & Moved some code to other files
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / markup-processor-helper-functions.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
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
18 // FILE HEADER
19 #include <dali-toolkit/internal/text/markup-processor-helper-functions.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/common/constants.h>
23 #include <dali/public-api/math/vector2.h>
24 #include <stdlib.h>
25 #include <iomanip>
26 #include <sstream>
27
28 namespace Dali
29 {
30 namespace Toolkit
31 {
32 namespace Text
33 {
34 namespace
35 {
36 const char WHITE_SPACE      = 0x20; // ASCII value of the white space.
37 const char FIRST_UPPER_CASE = 0x41; // ASCII value of the one after the first upper case character (A).
38 const char LAST_UPPER_CASE  = 0x5b; // ASCII value of the one after the last upper case character (Z).
39 const char TO_LOWER_CASE    = 32;   // Value to add to a upper case character to transform it into a lower case.
40
41 const char        WEB_COLOR_TOKEN('#');
42 const char* const HEX_COLOR_TOKEN("0x");
43 const char* const ALPHA_ONE("FF");
44
45 const std::string BLACK_COLOR("black");
46 const std::string WHITE_COLOR("white");
47 const std::string RED_COLOR("red");
48 const std::string GREEN_COLOR("green");
49 const std::string BLUE_COLOR("blue");
50 const std::string YELLOW_COLOR("yellow");
51 const std::string MAGENTA_COLOR("magenta");
52 const std::string CYAN_COLOR("cyan");
53 const std::string TRANSPARENT_COLOR("transparent");
54 } // namespace
55
56 bool TokenComparison(const std::string& string1, const char* const stringBuffer2, Length length)
57 {
58   const Length stringSize = string1.size();
59   if(stringSize != length)
60   {
61     // Early return. Strings have different sizes.
62     return false;
63   }
64
65   const char* const stringBuffer1 = string1.c_str();
66
67   for(std::size_t index = 0; index < stringSize; ++index)
68   {
69     const char character = *(stringBuffer2 + index);
70     const bool toLower   = (character < LAST_UPPER_CASE) && (character >= FIRST_UPPER_CASE);
71     if(*(stringBuffer1 + index) != (toLower ? character + TO_LOWER_CASE : character))
72     {
73       return false;
74     }
75   }
76
77   return true;
78 }
79
80 void SkipWhiteSpace(const char*&      stringBuffer,
81                     const char* const stringEndBuffer)
82 {
83   for(; (WHITE_SPACE >= *stringBuffer) && (stringBuffer < stringEndBuffer); ++stringBuffer)
84     ;
85 }
86
87 void JumpToWhiteSpace(const char*&      stringBuffer,
88                       const char* const stringEndBuffer)
89 {
90   for(; (WHITE_SPACE != *stringBuffer) && (stringBuffer < stringEndBuffer); ++stringBuffer)
91     ;
92 }
93
94 unsigned int StringToUint(const char* const uintStr)
95 {
96   return static_cast<unsigned int>(strtoul(uintStr, NULL, 10));
97 }
98
99 unsigned int StringToHex(const char* const uintStr)
100 {
101   return static_cast<unsigned int>(strtoul(uintStr, NULL, 16));
102 }
103
104 float StringToFloat(const char* const floatStr)
105 {
106   return static_cast<float>(strtod(floatStr, NULL));
107 }
108
109 void FloatToString(float value, std::string& floatStr)
110 {
111   std::stringstream ss;
112   ss << value;
113   floatStr = ss.str();
114 }
115
116 void UintToString(unsigned int value, std::string& uIntStr)
117 {
118   std::stringstream ss;
119   ss << value;
120   uIntStr = ss.str();
121 }
122
123 void UintColorToVector4(unsigned int color, Vector4& retColor)
124 {
125   retColor.a = static_cast<float>((color & 0xFF000000) >> 24u) / 255.f;
126   retColor.r = static_cast<float>((color & 0x00FF0000) >> 16u) / 255.f;
127   retColor.g = static_cast<float>((color & 0x0000FF00) >> 8u) / 255.f;
128   retColor.b = static_cast<float>(color & 0x000000FF) / 255.f;
129 }
130
131 void ColorStringToVector4(const char* const colorStr, Length length, Vector4& retColor)
132 {
133   if(WEB_COLOR_TOKEN == *colorStr)
134   {
135     std::string webColor(colorStr + 1u, length - 1u);
136     if(4u == length) // 3 component web color #F00 (red)
137     {
138       webColor.insert(2u, &(webColor[2]), 1u);
139       webColor.insert(1u, &(webColor[1]), 1u);
140       webColor.insert(0u, &(webColor[0]), 1u);
141       webColor.insert(0u, ALPHA_ONE);
142     }
143     else if(7u == length) // 6 component web color #FF0000 (red)
144     {
145       webColor.insert(0u, ALPHA_ONE);
146     }
147
148     UintColorToVector4(StringToHex(webColor.c_str()), retColor);
149   }
150   else if(TokenComparison(HEX_COLOR_TOKEN, colorStr, 2u))
151   {
152     UintColorToVector4(StringToHex(colorStr + 2u), retColor);
153   }
154   else if(TokenComparison(BLACK_COLOR, colorStr, length))
155   {
156     retColor = Color::BLACK;
157   }
158   else if(TokenComparison(WHITE_COLOR, colorStr, length))
159   {
160     retColor = Color::WHITE;
161   }
162   else if(TokenComparison(RED_COLOR, colorStr, length))
163   {
164     retColor = Color::RED;
165   }
166   else if(TokenComparison(GREEN_COLOR, colorStr, length))
167   {
168     retColor = Color::GREEN;
169   }
170   else if(TokenComparison(BLUE_COLOR, colorStr, length))
171   {
172     retColor = Color::BLUE;
173   }
174   else if(TokenComparison(YELLOW_COLOR, colorStr, length))
175   {
176     retColor = Color::YELLOW;
177   }
178   else if(TokenComparison(MAGENTA_COLOR, colorStr, length))
179   {
180     retColor = Color::MAGENTA;
181   }
182   else if(TokenComparison(CYAN_COLOR, colorStr, length))
183   {
184     retColor = Color::CYAN;
185   }
186   else if(TokenComparison(TRANSPARENT_COLOR, colorStr, length))
187   {
188     retColor = Color::TRANSPARENT;
189   }
190 }
191
192 void Vector4ToColorString(const Vector4& value, std::string& vector2Str)
193 {
194   if(Color::BLACK == value)
195   {
196     vector2Str = BLACK_COLOR;
197     return;
198   }
199
200   if(Color::WHITE == value)
201   {
202     vector2Str = WHITE_COLOR;
203     return;
204   }
205
206   if(Color::RED == value)
207   {
208     vector2Str = RED_COLOR;
209     return;
210   }
211
212   if(Color::GREEN == value)
213   {
214     vector2Str = GREEN_COLOR;
215     return;
216   }
217
218   if(Color::BLUE == value)
219   {
220     vector2Str = BLUE_COLOR;
221     return;
222   }
223
224   if(Color::YELLOW == value)
225   {
226     vector2Str = YELLOW_COLOR;
227     return;
228   }
229
230   if(Color::MAGENTA == value)
231   {
232     vector2Str = MAGENTA_COLOR;
233     return;
234   }
235
236   if(Color::CYAN == value)
237   {
238     vector2Str = CYAN_COLOR;
239     return;
240   }
241
242   if(Color::TRANSPARENT == value)
243   {
244     vector2Str = TRANSPARENT_COLOR;
245     return;
246   }
247
248   const unsigned int alpha = static_cast<unsigned int>(255.f * value.a);
249   const unsigned int red   = static_cast<unsigned int>(255.f * value.r);
250   const unsigned int green = static_cast<unsigned int>(255.f * value.g);
251   const unsigned int blue  = static_cast<unsigned int>(255.f * value.b);
252
253   std::stringstream  ss;
254   const unsigned int size = 2u * sizeof(unsigned char);
255
256   ss << "0x"
257      << std::setfill('0') << std::setw(size)
258      << std::hex << alpha
259      << std::setfill('0') << std::setw(size)
260      << std::hex << red
261      << std::setfill('0') << std::setw(size)
262      << std::hex << green
263      << std::setfill('0') << std::setw(size)
264      << std::hex << blue;
265   vector2Str = ss.str();
266 }
267
268 void StringToVector2(const char* const vectorStr, Length length, Vector2& vector2)
269 {
270   // Points to the first character of the string.
271   const char* strBuffer = vectorStr;
272
273   // Points to the first character of the 'x' value.
274   const char* const xBuffer = strBuffer;
275
276   // Jumps to the next white space.
277   JumpToWhiteSpace(strBuffer, strBuffer + length);
278
279   // Points to the first character of the 'y' value.
280   const char* const yBuffer = strBuffer;
281
282   // Converts the shadow's offset to float.
283   vector2.x = StringToFloat(xBuffer);
284   vector2.y = StringToFloat(yBuffer);
285 }
286
287 void Vector2ToString(const Vector2& value, std::string& vector2Str)
288 {
289   FloatToString(value.x, vector2Str);
290   vector2Str += " ";
291
292   std::string yStr;
293   FloatToString(value.y, yStr);
294
295   vector2Str += yStr;
296 }
297
298 } // namespace Text
299
300 } // namespace Toolkit
301
302 } // namespace Dali