e4cb1c4ee6942944c479ed18636c07c2676e59c2
[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 unsigned int MAX_FLOAT_ATTRIBUTE_SIZE = 17u; ///< The maximum length of any of the possible float values.  +99999.999999999f  (sign, five digits, dot, nine digits, f)
42
43 const char        WEB_COLOR_TOKEN('#');
44 const char* const HEX_COLOR_TOKEN("0x");
45 const char* const ALPHA_ONE("FF");
46
47 const std::string BLACK_COLOR("black");
48 const std::string WHITE_COLOR("white");
49 const std::string RED_COLOR("red");
50 const std::string GREEN_COLOR("green");
51 const std::string BLUE_COLOR("blue");
52 const std::string YELLOW_COLOR("yellow");
53 const std::string MAGENTA_COLOR("magenta");
54 const std::string CYAN_COLOR("cyan");
55 const std::string TRANSPARENT_COLOR("transparent");
56
57 const std::string SOLID_UNDERLINE("solid");
58 const std::string DASHED_UNDERLINE("dashed");
59 const std::string DOUBLE_UNDERLINE("double");
60 } // namespace
61
62 bool TokenComparison(const std::string& string1, const char* const stringBuffer2, Length length)
63 {
64   const Length stringSize = string1.size();
65   if(stringSize != length)
66   {
67     // Early return. Strings have different sizes.
68     return false;
69   }
70
71   const char* const stringBuffer1 = string1.c_str();
72
73   for(std::size_t index = 0; index < stringSize; ++index)
74   {
75     const char character = *(stringBuffer2 + index);
76     const bool toLower   = (character < LAST_UPPER_CASE) && (character >= FIRST_UPPER_CASE);
77     if(*(stringBuffer1 + index) != (toLower ? character + TO_LOWER_CASE : character))
78     {
79       return false;
80     }
81   }
82
83   return true;
84 }
85
86 void SkipWhiteSpace(const char*&      stringBuffer,
87                     const char* const stringEndBuffer)
88 {
89   for(; (WHITE_SPACE >= *stringBuffer) && (stringBuffer < stringEndBuffer); ++stringBuffer)
90     ;
91 }
92
93 void JumpToWhiteSpace(const char*&      stringBuffer,
94                       const char* const stringEndBuffer)
95 {
96   for(; (WHITE_SPACE != *stringBuffer) && (stringBuffer < stringEndBuffer); ++stringBuffer)
97     ;
98 }
99
100 unsigned int StringToUint(const char* const uintStr)
101 {
102   return static_cast<unsigned int>(strtoul(uintStr, NULL, 10));
103 }
104
105 unsigned int StringToHex(const char* const uintStr)
106 {
107   return static_cast<unsigned int>(strtoul(uintStr, NULL, 16));
108 }
109
110 float StringToFloat(const char* const floatStr)
111 {
112   return static_cast<float>(strtod(floatStr, NULL));
113 }
114
115 void FloatToString(float value, std::string& floatStr)
116 {
117   std::stringstream ss;
118   ss << value;
119   floatStr = ss.str();
120 }
121
122 void UintToString(unsigned int value, std::string& uIntStr)
123 {
124   std::stringstream ss;
125   ss << value;
126   uIntStr = ss.str();
127 }
128
129 void UintColorToVector4(unsigned int color, Vector4& retColor)
130 {
131   retColor.a = static_cast<float>((color & 0xFF000000) >> 24u) / 255.f;
132   retColor.r = static_cast<float>((color & 0x00FF0000) >> 16u) / 255.f;
133   retColor.g = static_cast<float>((color & 0x0000FF00) >> 8u) / 255.f;
134   retColor.b = static_cast<float>(color & 0x000000FF) / 255.f;
135 }
136
137 void ColorStringToVector4(const char* const colorStr, Length length, Vector4& retColor)
138 {
139   if(WEB_COLOR_TOKEN == *colorStr)
140   {
141     std::string webColor(colorStr + 1u, length - 1u);
142     if(4u == length) // 3 component web color #F00 (red)
143     {
144       webColor.insert(2u, &(webColor[2]), 1u);
145       webColor.insert(1u, &(webColor[1]), 1u);
146       webColor.insert(0u, &(webColor[0]), 1u);
147       webColor.insert(0u, ALPHA_ONE);
148     }
149     else if(7u == length) // 6 component web color #FF0000 (red)
150     {
151       webColor.insert(0u, ALPHA_ONE);
152     }
153
154     UintColorToVector4(StringToHex(webColor.c_str()), retColor);
155   }
156   else if(TokenComparison(HEX_COLOR_TOKEN, colorStr, 2u))
157   {
158     UintColorToVector4(StringToHex(colorStr + 2u), retColor);
159   }
160   else if(TokenComparison(BLACK_COLOR, colorStr, length))
161   {
162     retColor = Color::BLACK;
163   }
164   else if(TokenComparison(WHITE_COLOR, colorStr, length))
165   {
166     retColor = Color::WHITE;
167   }
168   else if(TokenComparison(RED_COLOR, colorStr, length))
169   {
170     retColor = Color::RED;
171   }
172   else if(TokenComparison(GREEN_COLOR, colorStr, length))
173   {
174     retColor = Color::GREEN;
175   }
176   else if(TokenComparison(BLUE_COLOR, colorStr, length))
177   {
178     retColor = Color::BLUE;
179   }
180   else if(TokenComparison(YELLOW_COLOR, colorStr, length))
181   {
182     retColor = Color::YELLOW;
183   }
184   else if(TokenComparison(MAGENTA_COLOR, colorStr, length))
185   {
186     retColor = Color::MAGENTA;
187   }
188   else if(TokenComparison(CYAN_COLOR, colorStr, length))
189   {
190     retColor = Color::CYAN;
191   }
192   else if(TokenComparison(TRANSPARENT_COLOR, colorStr, length))
193   {
194     retColor = Color::TRANSPARENT;
195   }
196 }
197
198 void Vector4ToColorString(const Vector4& value, std::string& vector2Str)
199 {
200   if(Color::BLACK == value)
201   {
202     vector2Str = BLACK_COLOR;
203     return;
204   }
205
206   if(Color::WHITE == value)
207   {
208     vector2Str = WHITE_COLOR;
209     return;
210   }
211
212   if(Color::RED == value)
213   {
214     vector2Str = RED_COLOR;
215     return;
216   }
217
218   if(Color::GREEN == value)
219   {
220     vector2Str = GREEN_COLOR;
221     return;
222   }
223
224   if(Color::BLUE == value)
225   {
226     vector2Str = BLUE_COLOR;
227     return;
228   }
229
230   if(Color::YELLOW == value)
231   {
232     vector2Str = YELLOW_COLOR;
233     return;
234   }
235
236   if(Color::MAGENTA == value)
237   {
238     vector2Str = MAGENTA_COLOR;
239     return;
240   }
241
242   if(Color::CYAN == value)
243   {
244     vector2Str = CYAN_COLOR;
245     return;
246   }
247
248   if(Color::TRANSPARENT == value)
249   {
250     vector2Str = TRANSPARENT_COLOR;
251     return;
252   }
253
254   const unsigned int alpha = static_cast<unsigned int>(255.f * value.a);
255   const unsigned int red   = static_cast<unsigned int>(255.f * value.r);
256   const unsigned int green = static_cast<unsigned int>(255.f * value.g);
257   const unsigned int blue  = static_cast<unsigned int>(255.f * value.b);
258
259   std::stringstream  ss;
260   const unsigned int size = 2u * sizeof(unsigned char);
261
262   ss << "0x"
263      << std::setfill('0') << std::setw(size)
264      << std::hex << alpha
265      << std::setfill('0') << std::setw(size)
266      << std::hex << red
267      << std::setfill('0') << std::setw(size)
268      << std::hex << green
269      << std::setfill('0') << std::setw(size)
270      << std::hex << blue;
271   vector2Str = ss.str();
272 }
273
274 void StringToVector2(const char* const vectorStr, Length length, Vector2& vector2)
275 {
276   // Points to the first character of the string.
277   const char* strBuffer = vectorStr;
278
279   // Points to the first character of the 'x' value.
280   const char* const xBuffer = strBuffer;
281
282   // Jumps to the next white space.
283   JumpToWhiteSpace(strBuffer, strBuffer + length);
284
285   // Points to the first character of the 'y' value.
286   const char* const yBuffer = strBuffer;
287
288   // Converts the shadow's offset to float.
289   vector2.x = StringToFloat(xBuffer);
290   vector2.y = StringToFloat(yBuffer);
291 }
292
293 void Vector2ToString(const Vector2& value, std::string& vector2Str)
294 {
295   FloatToString(value.x, vector2Str);
296   vector2Str += " ";
297
298   std::string yStr;
299   FloatToString(value.y, yStr);
300
301   vector2Str += yStr;
302 }
303
304 void UnderlineTypeStringToTypeValue(const char* const typeStr, Length length, Text::Underline::Type& retType)
305 {
306   if(TokenComparison(SOLID_UNDERLINE, typeStr, length))
307   {
308     retType = Text::Underline::SOLID;
309   }
310   else if(TokenComparison(DASHED_UNDERLINE, typeStr, length))
311   {
312     retType = Text::Underline::DASHED;
313   }
314   else if(TokenComparison(DOUBLE_UNDERLINE, typeStr, length))
315   {
316     retType = Text::Underline::DOUBLE;
317   }
318 }
319
320 } // namespace Text
321
322 } // namespace Toolkit
323
324 } // namespace Dali