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>
36 const char WHITE_SPACE = 0x20; // ASCII value of the white space.
37 const char LAST_UPPER_CASE = 0x5b; // ASCII value of the one after the last upper case character (Z).
38 const char TO_LOWER_CASE = 32; // Value to add to a upper case character to transform it into a lower case.
40 const char WEB_COLOR_TOKEN( '#' );
41 const char* const HEX_COLOR_TOKEN( "0x" );
42 const char* const ALPHA_ONE( "FF" );
44 const std::string BLACK_COLOR( "black" );
45 const std::string WHITE_COLOR( "white" );
46 const std::string RED_COLOR( "red" );
47 const std::string GREEN_COLOR( "green" );
48 const std::string BLUE_COLOR( "blue" );
49 const std::string YELLOW_COLOR( "yellow" );
50 const std::string MAGENTA_COLOR( "magenta" );
51 const std::string CYAN_COLOR( "cyan" );
52 const std::string TRANSPARENT_COLOR( "transparent" );
55 bool TokenComparison( const std::string& string1, const char* const stringBuffer2, Length length )
57 const Length stringSize = string1.size();
58 if( stringSize != length )
60 // Early return. Strings have different sizes.
64 const char* const stringBuffer1 = string1.c_str();
66 for( std::size_t index = 0; index < stringSize; ++index )
68 char character = *( stringBuffer2 + index );
69 if( *( stringBuffer1 + index ) != ( ( ( character < LAST_UPPER_CASE ) && ( '0' != character ) ) ? character + TO_LOWER_CASE : character ) )
78 void SkipWhiteSpace( const char*& markupStringBuffer,
79 const char* const markupStringEndBuffer )
81 for( ; ( WHITE_SPACE >= *markupStringBuffer ) && ( markupStringBuffer < markupStringEndBuffer ); ++markupStringBuffer );
84 unsigned int StringToHex( const char* const uintStr )
86 return static_cast<unsigned int>( strtoul( uintStr, NULL, 16 ) );
89 float StringToFloat( const char* const floatStr )
91 return static_cast<float>( strtod( floatStr, NULL ) );
94 void UintColorToVector4( unsigned int color, Vector4& retColor )
96 retColor.a = static_cast<float>( ( color & 0xFF000000 ) >> 24u ) / 255.f;
97 retColor.r = static_cast<float>( ( color & 0x00FF0000 ) >> 16u ) / 255.f;
98 retColor.g = static_cast<float>( ( color & 0x0000FF00 ) >> 8u ) / 255.f;
99 retColor.b = static_cast<float>( color & 0x000000FF ) / 255.f;
102 void ColorStringToVector4( const char* const colorStr, Length length, Vector4& retColor )
104 if( WEB_COLOR_TOKEN == *colorStr )
106 std::string webColor( colorStr + 1u, length - 1u );
107 if( 4u == length ) // 3 component web color #F00 (red)
109 webColor.insert( 2u, &( webColor[2] ), 1u );
110 webColor.insert( 1u, &( webColor[1] ), 1u );
111 webColor.insert( 0u, &( webColor[0] ), 1u );
112 webColor.insert( 0u, ALPHA_ONE );
114 else if( 7u == length ) // 6 component web color #FF0000 (red)
116 webColor.insert( 0u, ALPHA_ONE );
119 UintColorToVector4( StringToHex( webColor.c_str() ), retColor );
121 else if( TokenComparison( HEX_COLOR_TOKEN, colorStr, 2u ) )
123 UintColorToVector4( StringToHex( colorStr + 2u ), retColor );
125 else if( TokenComparison( BLACK_COLOR, colorStr, length ) )
127 retColor = Color::BLACK;
129 else if( TokenComparison( WHITE_COLOR, colorStr, length ) )
131 retColor = Color::WHITE;
133 else if( TokenComparison( RED_COLOR, colorStr, length ) )
135 retColor = Color::RED;
137 else if( TokenComparison( GREEN_COLOR, colorStr, length ) )
139 retColor = Color::GREEN;
141 else if( TokenComparison( BLUE_COLOR, colorStr, length ) )
143 retColor = Color::BLUE;
145 else if( TokenComparison( YELLOW_COLOR, colorStr, length ) )
147 retColor = Color::YELLOW;
149 else if( TokenComparison( MAGENTA_COLOR, colorStr, length ) )
151 retColor = Color::MAGENTA;
153 else if( TokenComparison( CYAN_COLOR, colorStr, length ) )
155 retColor = Color::CYAN;
157 else if( TokenComparison( TRANSPARENT_COLOR, colorStr, length ) )
159 retColor = Color::TRANSPARENT;
165 } // namespace Toolkit