Markup procesor - Font.
[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 <stdlib.h>
24
25 namespace Dali
26 {
27
28 namespace Toolkit
29 {
30
31 namespace Text
32 {
33
34 namespace
35 {
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.
39
40 const char WEB_COLOR_TOKEN( '#' );
41 const char* const HEX_COLOR_TOKEN( "0x" );
42 const char* const ALPHA_ONE( "FF" );
43
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" );
53 }
54
55 bool TokenComparison( const std::string& string1, const char* const stringBuffer2, Length length )
56 {
57   const Length stringSize = string1.size();
58   if( stringSize != length )
59   {
60     // Early return. Strings have different sizes.
61     return false;
62   }
63
64   const char* const stringBuffer1 = string1.c_str();
65
66   for( std::size_t index = 0; index < stringSize; ++index )
67   {
68     char character = *( stringBuffer2 + index );
69     if( *( stringBuffer1 + index ) != ( ( ( character < LAST_UPPER_CASE ) && ( '0' != character ) ) ? character + TO_LOWER_CASE : character ) )
70     {
71       return false;
72     }
73   }
74
75   return true;
76 }
77
78 void SkipWhiteSpace( const char*& markupStringBuffer,
79                      const char* const markupStringEndBuffer )
80 {
81   for( ; ( WHITE_SPACE >= *markupStringBuffer ) && ( markupStringBuffer < markupStringEndBuffer ); ++markupStringBuffer );
82 }
83
84 unsigned int StringToHex( const char* const uintStr )
85 {
86   return static_cast<unsigned int>( strtoul( uintStr, NULL, 16 ) );
87 }
88
89 float StringToFloat( const char* const floatStr )
90 {
91   return static_cast<float>( strtod( floatStr, NULL ) );
92 }
93
94 void UintColorToVector4( unsigned int color, Vector4& retColor )
95 {
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;
100 }
101
102 void ColorStringToVector4( const char* const colorStr, Length length, Vector4& retColor )
103 {
104   if( WEB_COLOR_TOKEN == *colorStr )
105   {
106     std::string webColor( colorStr + 1u, length - 1u );
107     if( 4u == length )                      // 3 component web color #F00 (red)
108     {
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 );
113     }
114     else if( 7u == length )                 // 6 component web color #FF0000 (red)
115     {
116       webColor.insert( 0u, ALPHA_ONE );
117     }
118
119     UintColorToVector4( StringToHex( webColor.c_str() ), retColor );
120   }
121   else if( TokenComparison( HEX_COLOR_TOKEN, colorStr, 2u ) )
122   {
123     UintColorToVector4( StringToHex( colorStr + 2u ), retColor );
124   }
125   else if( TokenComparison( BLACK_COLOR, colorStr, length ) )
126   {
127     retColor = Color::BLACK;
128   }
129   else if( TokenComparison( WHITE_COLOR, colorStr, length ) )
130   {
131     retColor = Color::WHITE;
132   }
133   else if( TokenComparison( RED_COLOR, colorStr, length ) )
134   {
135     retColor = Color::RED;
136   }
137   else if( TokenComparison( GREEN_COLOR, colorStr, length ) )
138   {
139     retColor = Color::GREEN;
140   }
141   else if( TokenComparison( BLUE_COLOR, colorStr, length ) )
142   {
143     retColor = Color::BLUE;
144   }
145   else if( TokenComparison( YELLOW_COLOR, colorStr, length ) )
146   {
147     retColor = Color::YELLOW;
148   }
149   else if( TokenComparison( MAGENTA_COLOR, colorStr, length ) )
150   {
151     retColor = Color::MAGENTA;
152   }
153   else if( TokenComparison( CYAN_COLOR, colorStr, length ) )
154   {
155     retColor = Color::CYAN;
156   }
157   else if( TokenComparison( TRANSPARENT_COLOR, colorStr, length ) )
158   {
159     retColor = Color::TRANSPARENT;
160   }
161 }
162
163 } // namespace Text
164
165 } // namespace Toolkit
166
167 } // namespace Dali