Svace issues.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-font-style.cpp
1 /*
2  * Copyright (c) 2016 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/text-font-style.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/devel-api/builder/json-parser.h>
26 #include <dali-toolkit/devel-api/builder/tree-node.h>
27
28 namespace Dali
29 {
30
31 namespace Toolkit
32 {
33
34 namespace Text
35 {
36
37 namespace
38 {
39 const std::string STYLE_KEY( "style" );
40 const std::string WEIGHT_KEY( "weight" );
41 const std::string WIDTH_KEY( "width" );
42 const std::string SLANT_KEY( "slant" );
43 const std::string EMPTY_STRING( "" );
44
45 #if defined(DEBUG_ENABLED)
46 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_CONTROLS");
47 #endif
48
49 } // namespace
50
51 /**
52  * @brief Creates a map with pairs 'key,value' with the font's style parameters.
53  *
54  * @param[in] node Data structure with the font's style parameters.
55  * @param[out] map A map with the font's style parameters.
56  *
57  */
58 void CreateFontStyleMap( const TreeNode* const node, Property::Map& map )
59 {
60   switch( node->GetType() )
61   {
62     case TreeNode::IS_NULL:
63     case TreeNode::OBJECT:
64     case TreeNode::ARRAY: // FALL THROUGH
65     {
66       break;
67     }
68     case TreeNode::STRING:
69     {
70       map.Insert( node->GetName(), Property::Value( node->GetString() ) );
71       break;
72     }
73     case TreeNode::INTEGER:
74     case TreeNode::FLOAT:
75     case TreeNode::BOOLEAN: // FALL THROUGH
76     {
77       break;
78     }
79   }
80
81   for( TreeNode::ConstIterator it = node->CBegin(), endIt = node->CEnd(); it != endIt; ++it )
82   {
83     const TreeNode::KeyNodePair& pair = *it;
84     CreateFontStyleMap( &pair.second, map );
85   }
86 }
87
88 /**
89  * @brief Parses the font's style string.
90  *
91  * @param[in] style The font's style string.
92  * @param[out] map A map with the font's style parameters.
93  *
94  */
95 void ParseFontStyleString( const std::string& style, Property::Map& map )
96 {
97   Toolkit::JsonParser parser = Toolkit::JsonParser::New();
98
99   if( parser.Parse( style ) )
100   {
101     const TreeNode* const node = parser.GetRoot();
102
103     if( NULL != node )
104     {
105       CreateFontStyleMap( node, map );
106     }
107   }
108 }
109
110 void SetFontStyleProperty( ControllerPtr controller, const Property::Value& value, FontStyle::Type type )
111 {
112   if( controller )
113   {
114     const std::string style = value.Get< std::string >();
115     DALI_LOG_INFO( gLogFilter, Debug::General, "Text Control %p FONT_STYLE %s\n", controller.Get(), style.c_str() );
116
117     switch( type )
118     {
119       case FontStyle::DEFAULT:
120       {
121         // Stores the default font's style string to be recovered by the GetFontStyleProperty() function.
122         controller->SetDefaultFontStyle( style );
123         break;
124       }
125       case FontStyle::INPUT:
126       {
127         // Stores the input font's style string to be recovered by the GetFontStyleProperty() function.
128         controller->SetInputFontStyle( style );
129         break;
130       }
131     }
132
133     // Parses and applies the style.
134     Property::Map map;
135     ParseFontStyleString( style, map );
136
137     if( !map.Empty() )
138     {
139       /// Weight key
140       Property::Value* weightValue = map.Find( WEIGHT_KEY );
141
142       FontWeight weight = TextAbstraction::FontWeight::NORMAL;
143       const bool weightDefined = weightValue != NULL;
144       if( weightDefined )
145       {
146         const std::string weightStr = weightValue->Get<std::string>();
147
148         Scripting::GetEnumeration< FontWeight >( weightStr.c_str(),
149                                                  FONT_WEIGHT_STRING_TABLE,
150                                                  FONT_WEIGHT_STRING_TABLE_COUNT,
151                                                  weight );
152       }
153
154       /// Width key
155       Property::Value* widthValue = map.Find( WIDTH_KEY );
156
157       FontWidth width = TextAbstraction::FontWidth::NORMAL;
158       const bool widthDefined = widthValue != NULL;
159       if( widthDefined )
160       {
161         const std::string widthStr = widthValue->Get<std::string>();
162
163         Scripting::GetEnumeration< FontWidth >( widthStr.c_str(),
164                                                 FONT_WIDTH_STRING_TABLE,
165                                                 FONT_WIDTH_STRING_TABLE_COUNT,
166                                                 width );
167       }
168
169       /// Slant key
170       Property::Value* slantValue = map.Find( SLANT_KEY );
171
172       FontSlant slant = TextAbstraction::FontSlant::NORMAL;
173       const bool slantDefined = slantValue != NULL;
174       if( slantDefined )
175       {
176         const std::string slantStr = slantValue->Get<std::string>();
177
178         Scripting::GetEnumeration< FontSlant >( slantStr.c_str(),
179                                                 FONT_SLANT_STRING_TABLE,
180                                                 FONT_SLANT_STRING_TABLE_COUNT,
181                                                 slant );
182       }
183
184       switch( type )
185       {
186         case FontStyle::DEFAULT:
187         {
188           // Sets the default font's style values.
189           if( weightDefined && ( controller->GetDefaultFontWeight() != weight ) )
190           {
191             controller->SetDefaultFontWeight( weight );
192           }
193
194           if( widthDefined && ( controller->GetDefaultFontWidth() != width ) )
195           {
196             controller->SetDefaultFontWidth( width );
197           }
198
199           if( slantDefined && ( controller->GetDefaultFontSlant() != slant ) )
200           {
201             controller->SetDefaultFontSlant( slant );
202           }
203           break;
204         }
205         case FontStyle::INPUT:
206         {
207           // Sets the input font's style values.
208           if( weightDefined && ( controller->GetInputFontWeight() != weight ) )
209           {
210             controller->SetInputFontWeight( weight );
211           }
212
213           if( widthDefined && ( controller->GetInputFontWidth() != width ) )
214           {
215             controller->SetInputFontWidth( width );
216           }
217
218           if( slantDefined && ( controller->GetInputFontSlant() != slant ) )
219           {
220             controller->SetInputFontSlant( slant );
221           }
222           break;
223         }
224       }
225     }
226   }
227 }
228
229 void GetFontStyleProperty( ControllerPtr controller, Property::Value& value, FontStyle::Type type )
230 {
231   if( controller )
232   {
233     switch( type )
234     {
235       case FontStyle::DEFAULT:
236       {
237         value = controller->GetDefaultFontStyle();
238         break;
239       }
240       case FontStyle::INPUT:
241       {
242         value = controller->GetInputFontStyle();
243         break;
244       }
245     }
246   }
247 }
248
249 FontWeight StringToWeight( const char* const weightStr )
250 {
251   FontWeight weight = TextAbstraction::FontWeight::NORMAL;
252   Scripting::GetEnumeration<FontWeight>( weightStr,
253                                          FONT_WEIGHT_STRING_TABLE,
254                                          FONT_WEIGHT_STRING_TABLE_COUNT,
255                                          weight );
256
257   return weight;
258 }
259
260 FontWidth StringToWidth( const char* const widthStr )
261 {
262   FontWidth width = TextAbstraction::FontWidth::NORMAL;
263   Scripting::GetEnumeration<FontWidth>( widthStr,
264                                         FONT_WIDTH_STRING_TABLE,
265                                         FONT_WIDTH_STRING_TABLE_COUNT,
266                                         width );
267
268   return width;
269 }
270
271 FontSlant StringToSlant( const char* const slantStr )
272 {
273   FontSlant slant = TextAbstraction::FontSlant::NORMAL;
274   Scripting::GetEnumeration<FontSlant>( slantStr,
275                                         FONT_SLANT_STRING_TABLE,
276                                         FONT_SLANT_STRING_TABLE_COUNT,
277                                         slant );
278
279   return slant;
280 }
281
282 } // namespace Text
283
284 } // namespace Toolkit
285
286 } // namespace Dali