2 * Copyright (c) 2016 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/text-font-style.h>
22 #include <dali/integration-api/debug.h>
25 #include <dali-toolkit/devel-api/builder/json-parser.h>
26 #include <dali-toolkit/devel-api/builder/tree-node.h>
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( "" );
45 #if defined(DEBUG_ENABLED)
46 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_CONTROLS");
52 * @brief Creates a map with pairs 'key,value' with the font's style parameters.
54 * @param[in] node Data structure with the font's style parameters.
55 * @param[out] map A map with the font's style parameters.
58 void CreateFontStyleMap( const TreeNode* const node, Property::Map& map )
60 switch( node->GetType() )
62 case TreeNode::IS_NULL:
63 case TreeNode::OBJECT:
64 case TreeNode::ARRAY: // FALL THROUGH
68 case TreeNode::STRING:
70 map.Insert( node->GetName(), Property::Value( node->GetString() ) );
73 case TreeNode::INTEGER:
75 case TreeNode::BOOLEAN: // FALL THROUGH
81 for( TreeNode::ConstIterator it = node->CBegin(), endIt = node->CEnd(); it != endIt; ++it )
83 const TreeNode::KeyNodePair& pair = *it;
84 CreateFontStyleMap( &pair.second, map );
89 * @brief Parses the font's style string.
91 * @param[in] style The font's style string.
92 * @param[out] map A map with the font's style parameters.
95 void ParseFontStyleString( const std::string& style, Property::Map& map )
97 Toolkit::JsonParser parser = Toolkit::JsonParser::New();
99 if( parser.Parse( style ) )
101 const TreeNode* const node = parser.GetRoot();
105 CreateFontStyleMap( node, map );
110 void SetFontStyleProperty( ControllerPtr controller, const Property::Value& value, FontStyle::Type type )
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() );
119 case FontStyle::DEFAULT:
121 // Stores the default font's style string to be recovered by the GetFontStyleProperty() function.
122 controller->SetDefaultFontStyle( style );
125 case FontStyle::INPUT:
127 // Stores the input font's style string to be recovered by the GetFontStyleProperty() function.
128 controller->SetInputFontStyle( style );
133 // Parses and applies the style.
135 ParseFontStyleString( style, map );
140 Property::Value* weightValue = map.Find( WEIGHT_KEY );
142 FontWeight weight = TextAbstraction::FontWeight::NORMAL;
143 const bool weightDefined = weightValue != NULL;
146 const std::string weightStr = weightValue->Get<std::string>();
148 Scripting::GetEnumeration< FontWeight >( weightStr.c_str(),
149 FONT_WEIGHT_STRING_TABLE,
150 FONT_WEIGHT_STRING_TABLE_COUNT,
155 Property::Value* widthValue = map.Find( WIDTH_KEY );
157 FontWidth width = TextAbstraction::FontWidth::NORMAL;
158 const bool widthDefined = widthValue != NULL;
161 const std::string widthStr = widthValue->Get<std::string>();
163 Scripting::GetEnumeration< FontWidth >( widthStr.c_str(),
164 FONT_WIDTH_STRING_TABLE,
165 FONT_WIDTH_STRING_TABLE_COUNT,
170 Property::Value* slantValue = map.Find( SLANT_KEY );
172 FontSlant slant = TextAbstraction::FontSlant::NORMAL;
173 const bool slantDefined = slantValue != NULL;
176 const std::string slantStr = slantValue->Get<std::string>();
178 Scripting::GetEnumeration< FontSlant >( slantStr.c_str(),
179 FONT_SLANT_STRING_TABLE,
180 FONT_SLANT_STRING_TABLE_COUNT,
186 case FontStyle::DEFAULT:
188 // Sets the default font's style values.
189 if( weightDefined && ( controller->GetDefaultFontWeight() != weight ) )
191 controller->SetDefaultFontWeight( weight );
194 if( widthDefined && ( controller->GetDefaultFontWidth() != width ) )
196 controller->SetDefaultFontWidth( width );
199 if( slantDefined && ( controller->GetDefaultFontSlant() != slant ) )
201 controller->SetDefaultFontSlant( slant );
205 case FontStyle::INPUT:
207 // Sets the input font's style values.
208 if( weightDefined && ( controller->GetInputFontWeight() != weight ) )
210 controller->SetInputFontWeight( weight );
213 if( widthDefined && ( controller->GetInputFontWidth() != width ) )
215 controller->SetInputFontWidth( width );
218 if( slantDefined && ( controller->GetInputFontSlant() != slant ) )
220 controller->SetInputFontSlant( slant );
229 void GetFontStyleProperty( ControllerPtr controller, Property::Value& value, FontStyle::Type type )
235 case FontStyle::DEFAULT:
237 value = controller->GetDefaultFontStyle();
240 case FontStyle::INPUT:
242 value = controller->GetInputFontStyle();
249 FontWeight StringToWeight( const char* const weightStr )
251 FontWeight weight = TextAbstraction::FontWeight::NORMAL;
252 Scripting::GetEnumeration<FontWeight>( weightStr,
253 FONT_WEIGHT_STRING_TABLE,
254 FONT_WEIGHT_STRING_TABLE_COUNT,
260 FontWidth StringToWidth( const char* const widthStr )
262 FontWidth width = TextAbstraction::FontWidth::NORMAL;
263 Scripting::GetEnumeration<FontWidth>( widthStr,
264 FONT_WIDTH_STRING_TABLE,
265 FONT_WIDTH_STRING_TABLE_COUNT,
271 FontSlant StringToSlant( const char* const slantStr )
273 FontSlant slant = TextAbstraction::FontSlant::NORMAL;
274 Scripting::GetEnumeration<FontSlant>( slantStr,
275 FONT_SLANT_STRING_TABLE,
276 FONT_SLANT_STRING_TABLE_COUNT,
284 } // namespace Toolkit