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/controls/text-controls/text-font-style.h>
22 #include <dali-toolkit/devel-api/builder/json-parser.h>
23 #include <dali-toolkit/devel-api/builder/tree-node.h>
36 const std::string STYLE_KEY( "style" );
37 const std::string WIDTH_KEY( "width" );
38 const std::string WEIGHT_KEY( "weight" );
39 const std::string SLANT_KEY( "slant" );
40 const std::string EMPTY_STRING( "" );
45 * @brief Creates a map with pairs 'key,value' with the font's style parameters.
47 * @param[in] node Data structure with the font's style parameters.
48 * @param[out] map A map with the font's style parameters.
51 void CreateFontStyleMap( const TreeNode* const node, Property::Map& map )
53 switch( node->GetType() )
55 case TreeNode::IS_NULL:
56 case TreeNode::OBJECT:
57 case TreeNode::ARRAY: // FALL THROUGH
61 case TreeNode::STRING:
63 map.Insert( node->GetName(), Property::Value( node->GetString() ) );
66 case TreeNode::INTEGER:
68 case TreeNode::BOOLEAN: // FALL THROUGH
74 for( TreeNode::ConstIterator it = node->CBegin(), endIt = node->CEnd(); it != endIt; ++it )
76 const TreeNode::KeyNodePair& pair = *it;
77 CreateFontStyleMap( &pair.second, map );
82 * @brief Parses the font's style string.
84 * @param[in] style The font's style string.
85 * @param[out] map A map with the font's style parameters.
88 void ParseFontStyleString( const std::string& style, Property::Map& map )
90 Toolkit::JsonParser parser = Toolkit::JsonParser::New();
92 if( parser.Parse( style ) )
94 const TreeNode* const node = parser.GetRoot();
96 CreateFontStyleMap( node, map );
100 void SetFontStyleProperty( ControllerPtr controller, const Property::Value& value )
104 const std::string style = value.Get< std::string >();
106 // Stores the string to be recovered by the GetFontStyleProperty() function.
107 controller->SetDefaultFontStyle( style );
109 // Parses and applies the style.
111 ParseFontStyleString( style, map );
116 Property::Value* widthValue = map.Find( WIDTH_KEY );
120 const std::string widthStr = widthValue->Get<std::string>();
122 FontWidth width = TextAbstraction::FontWidth::NORMAL;
123 if( Scripting::GetEnumeration< FontWidth >( widthStr.c_str(),
124 FONT_WIDTH_STRING_TABLE,
125 FONT_WIDTH_STRING_TABLE_COUNT,
128 if( controller->GetDefaultFontWidth() != width )
130 controller->SetDefaultFontWidth( width );
136 controller->SetDefaultFontWidth( TextAbstraction::FontWidth::NORMAL );
140 Property::Value* weightValue = map.Find( WEIGHT_KEY );
144 const std::string weightStr = weightValue->Get<std::string>();
146 FontWeight weight = TextAbstraction::FontWeight::NORMAL;
147 if( Scripting::GetEnumeration< FontWeight >( weightStr.c_str(),
148 FONT_WEIGHT_STRING_TABLE,
149 FONT_WEIGHT_STRING_TABLE_COUNT,
152 if( controller->GetDefaultFontWeight() != weight )
154 controller->SetDefaultFontWeight( weight );
160 controller->SetDefaultFontWeight( TextAbstraction::FontWeight::NORMAL );
164 Property::Value* slantValue = map.Find( SLANT_KEY );
168 const std::string slantStr = slantValue->Get<std::string>();
170 FontSlant slant = TextAbstraction::FontSlant::NORMAL;
171 if( Scripting::GetEnumeration< FontSlant >( slantStr.c_str(),
172 FONT_SLANT_STRING_TABLE,
173 FONT_SLANT_STRING_TABLE_COUNT,
176 if( controller->GetDefaultFontSlant() != slant )
178 controller->SetDefaultFontSlant( slant );
184 controller->SetDefaultFontSlant( TextAbstraction::FontSlant::NORMAL );
190 void GetFontStyleProperty( ControllerPtr controller, Property::Value& value )
194 value = controller->GetDefaultFontStyle();
200 } // namespace Toolkit