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/markup-processor-helper-functions.h>
22 #include <dali/public-api/common/constants.h>
23 #include <dali/public-api/math/vector2.h>
39 const char WHITE_SPACE = 0x20; // ASCII value of the white space.
40 const char FIRST_UPPER_CASE = 0x41; // ASCII value of the one after the first upper case character (A).
41 const char LAST_UPPER_CASE = 0x5b; // ASCII value of the one after the last upper case character (Z).
42 const char TO_LOWER_CASE = 32; // Value to add to a upper case character to transform it into a lower case.
44 const char WEB_COLOR_TOKEN( '#' );
45 const char* const HEX_COLOR_TOKEN( "0x" );
46 const char* const ALPHA_ONE( "FF" );
48 const std::string BLACK_COLOR( "black" );
49 const std::string WHITE_COLOR( "white" );
50 const std::string RED_COLOR( "red" );
51 const std::string GREEN_COLOR( "green" );
52 const std::string BLUE_COLOR( "blue" );
53 const std::string YELLOW_COLOR( "yellow" );
54 const std::string MAGENTA_COLOR( "magenta" );
55 const std::string CYAN_COLOR( "cyan" );
56 const std::string TRANSPARENT_COLOR( "transparent" );
59 bool TokenComparison( const std::string& string1, const char* const stringBuffer2, Length length )
61 const Length stringSize = string1.size();
62 if( stringSize != length )
64 // Early return. Strings have different sizes.
68 const char* const stringBuffer1 = string1.c_str();
70 for( std::size_t index = 0; index < stringSize; ++index )
72 const char character = *( stringBuffer2 + index );
73 const bool toLower = ( character < LAST_UPPER_CASE ) && ( character >= FIRST_UPPER_CASE );
74 if( *( stringBuffer1 + index ) != ( toLower ? character + TO_LOWER_CASE : character ) )
83 void SkipWhiteSpace( const char*& stringBuffer,
84 const char* const stringEndBuffer )
86 for( ; ( WHITE_SPACE >= *stringBuffer ) && ( stringBuffer < stringEndBuffer ); ++stringBuffer );
89 void JumpToWhiteSpace( const char*& stringBuffer,
90 const char* const stringEndBuffer )
92 for( ; ( WHITE_SPACE != *stringBuffer ) && ( stringBuffer < stringEndBuffer ); ++stringBuffer );
95 unsigned int StringToHex( const char* const uintStr )
97 return static_cast<unsigned int>( strtoul( uintStr, NULL, 16 ) );
100 float StringToFloat( const char* const floatStr )
102 return static_cast<float>( strtod( floatStr, NULL ) );
105 void FloatToString( float value, std::string& floatStr )
107 std::stringstream ss;
112 void UintColorToVector4( unsigned int color, Vector4& retColor )
114 retColor.a = static_cast<float>( ( color & 0xFF000000 ) >> 24u ) / 255.f;
115 retColor.r = static_cast<float>( ( color & 0x00FF0000 ) >> 16u ) / 255.f;
116 retColor.g = static_cast<float>( ( color & 0x0000FF00 ) >> 8u ) / 255.f;
117 retColor.b = static_cast<float>( color & 0x000000FF ) / 255.f;
120 void ColorStringToVector4( const char* const colorStr, Length length, Vector4& retColor )
122 if( WEB_COLOR_TOKEN == *colorStr )
124 std::string webColor( colorStr + 1u, length - 1u );
125 if( 4u == length ) // 3 component web color #F00 (red)
127 webColor.insert( 2u, &( webColor[2] ), 1u );
128 webColor.insert( 1u, &( webColor[1] ), 1u );
129 webColor.insert( 0u, &( webColor[0] ), 1u );
130 webColor.insert( 0u, ALPHA_ONE );
132 else if( 7u == length ) // 6 component web color #FF0000 (red)
134 webColor.insert( 0u, ALPHA_ONE );
137 UintColorToVector4( StringToHex( webColor.c_str() ), retColor );
139 else if( TokenComparison( HEX_COLOR_TOKEN, colorStr, 2u ) )
141 UintColorToVector4( StringToHex( colorStr + 2u ), retColor );
143 else if( TokenComparison( BLACK_COLOR, colorStr, length ) )
145 retColor = Color::BLACK;
147 else if( TokenComparison( WHITE_COLOR, colorStr, length ) )
149 retColor = Color::WHITE;
151 else if( TokenComparison( RED_COLOR, colorStr, length ) )
153 retColor = Color::RED;
155 else if( TokenComparison( GREEN_COLOR, colorStr, length ) )
157 retColor = Color::GREEN;
159 else if( TokenComparison( BLUE_COLOR, colorStr, length ) )
161 retColor = Color::BLUE;
163 else if( TokenComparison( YELLOW_COLOR, colorStr, length ) )
165 retColor = Color::YELLOW;
167 else if( TokenComparison( MAGENTA_COLOR, colorStr, length ) )
169 retColor = Color::MAGENTA;
171 else if( TokenComparison( CYAN_COLOR, colorStr, length ) )
173 retColor = Color::CYAN;
175 else if( TokenComparison( TRANSPARENT_COLOR, colorStr, length ) )
177 retColor = Color::TRANSPARENT;
181 void Vector4ToColorString( const Vector4& value, std::string& vector2Str )
183 if( Color::BLACK == value )
185 vector2Str = BLACK_COLOR;
189 if( Color::WHITE == value )
191 vector2Str = WHITE_COLOR;
195 if( Color::RED == value )
197 vector2Str = RED_COLOR;
201 if( Color::GREEN == value )
203 vector2Str = GREEN_COLOR;
207 if( Color::BLUE == value )
209 vector2Str = BLUE_COLOR;
213 if( Color::YELLOW == value )
215 vector2Str = YELLOW_COLOR;
219 if( Color::MAGENTA == value )
221 vector2Str = MAGENTA_COLOR;
225 if( Color::CYAN == value )
227 vector2Str = CYAN_COLOR;
231 if( Color::TRANSPARENT == value )
233 vector2Str = TRANSPARENT_COLOR;
237 const unsigned int alpha = static_cast<unsigned int>( 255.f * value.a );
238 const unsigned int red = static_cast<unsigned int>( 255.f * value.r );
239 const unsigned int green = static_cast<unsigned int>( 255.f * value.g );
240 const unsigned int blue = static_cast<unsigned int>( 255.f * value.b );
242 std::stringstream ss;
243 const unsigned int size = 2u * sizeof( unsigned char );
246 << std::setfill('0') << std::setw( size )
248 << std::setfill('0') << std::setw( size )
250 << std::setfill('0') << std::setw( size )
252 << std::setfill('0') << std::setw( size )
254 vector2Str = ss.str();
257 void StringToVector2( const char* const vectorStr, Length length, Vector2& vector2 )
259 // Points to the first character of the string.
260 const char* strBuffer = vectorStr;
262 // Points to the first character of the 'x' value.
263 const char* const xBuffer = strBuffer;
265 // Jumps to the next white space.
266 JumpToWhiteSpace( strBuffer, strBuffer + length );
268 // Points to the first character of the 'y' value.
269 const char* const yBuffer = strBuffer;
271 // Converts the shadow's offset to float.
272 vector2.x = StringToFloat( xBuffer );
273 vector2.y = StringToFloat( yBuffer );
276 void Vector2ToString( const Vector2& value, std::string& vector2Str )
278 FloatToString( value.x, vector2Str );
282 FloatToString( value.y, yStr );
289 } // namespace Toolkit