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();
103 CreateFontStyleMap( node, map );
107 void SetFontStyleProperty( ControllerPtr controller, const Property::Value& value, FontStyle::Type type )
111 const std::string style = value.Get< std::string >();
112 DALI_LOG_INFO( gLogFilter, Debug::General, "Text Control %p FONT_STYLE %s\n", controller.Get(), style.c_str() );
116 case FontStyle::DEFAULT:
118 // Stores the default font's style string to be recovered by the GetFontStyleProperty() function.
119 controller->SetDefaultFontStyle( style );
122 case FontStyle::INPUT:
124 // Stores the input font's style string to be recovered by the GetFontStyleProperty() function.
125 controller->SetInputFontStyle( style );
130 // Parses and applies the style.
132 ParseFontStyleString( style, map );
137 Property::Value* weightValue = map.Find( WEIGHT_KEY );
139 FontWeight weight = TextAbstraction::FontWeight::NORMAL;
140 const bool weightDefined = weightValue != NULL;
143 const std::string weightStr = weightValue->Get<std::string>();
145 Scripting::GetEnumeration< FontWeight >( weightStr.c_str(),
146 FONT_WEIGHT_STRING_TABLE,
147 FONT_WEIGHT_STRING_TABLE_COUNT,
152 Property::Value* widthValue = map.Find( WIDTH_KEY );
154 FontWidth width = TextAbstraction::FontWidth::NORMAL;
155 const bool widthDefined = widthValue != NULL;
158 const std::string widthStr = widthValue->Get<std::string>();
160 Scripting::GetEnumeration< FontWidth >( widthStr.c_str(),
161 FONT_WIDTH_STRING_TABLE,
162 FONT_WIDTH_STRING_TABLE_COUNT,
167 Property::Value* slantValue = map.Find( SLANT_KEY );
169 FontSlant slant = TextAbstraction::FontSlant::NORMAL;
170 const bool slantDefined = slantValue != NULL;
173 const std::string slantStr = slantValue->Get<std::string>();
175 Scripting::GetEnumeration< FontSlant >( slantStr.c_str(),
176 FONT_SLANT_STRING_TABLE,
177 FONT_SLANT_STRING_TABLE_COUNT,
183 case FontStyle::DEFAULT:
185 // Sets the default font's style values.
186 if( weightDefined && ( controller->GetDefaultFontWeight() != weight ) )
188 controller->SetDefaultFontWeight( weight );
191 if( widthDefined && ( controller->GetDefaultFontWidth() != width ) )
193 controller->SetDefaultFontWidth( width );
196 if( slantDefined && ( controller->GetDefaultFontSlant() != slant ) )
198 controller->SetDefaultFontSlant( slant );
202 case FontStyle::INPUT:
204 // Sets the input font's style values.
205 if( weightDefined && ( controller->GetInputFontWeight() != weight ) )
207 controller->SetInputFontWeight( weight );
210 if( widthDefined && ( controller->GetInputFontWidth() != width ) )
212 controller->SetInputFontWidth( width );
215 if( slantDefined && ( controller->GetInputFontSlant() != slant ) )
217 controller->SetInputFontSlant( slant );
226 void GetFontStyleProperty( ControllerPtr controller, Property::Value& value, FontStyle::Type type )
232 case FontStyle::DEFAULT:
234 value = controller->GetDefaultFontStyle();
237 case FontStyle::INPUT:
239 value = controller->GetInputFontStyle();
246 FontWeight StringToWeight( const char* const weightStr )
248 FontWeight weight = TextAbstraction::FontWeight::NORMAL;
249 Scripting::GetEnumeration<FontWeight>( weightStr,
250 FONT_WEIGHT_STRING_TABLE,
251 FONT_WEIGHT_STRING_TABLE_COUNT,
257 FontWidth StringToWidth( const char* const widthStr )
259 FontWidth width = TextAbstraction::FontWidth::NORMAL;
260 Scripting::GetEnumeration<FontWidth>( widthStr,
261 FONT_WIDTH_STRING_TABLE,
262 FONT_WIDTH_STRING_TABLE_COUNT,
268 FontSlant StringToSlant( const char* const slantStr )
270 FontSlant slant = TextAbstraction::FontSlant::NORMAL;
271 Scripting::GetEnumeration<FontSlant>( slantStr,
272 FONT_SLANT_STRING_TABLE,
273 FONT_SLANT_STRING_TABLE_COUNT,
281 } // namespace Toolkit