2c29f6849f1ff2edd3f1042e8746e81b79809790
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-font-style.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/controls/text-controls/text-font-style.h>
20
21 // INTERNAL INCLUDES
22 #include <dali-toolkit/devel-api/builder/json-parser.h>
23 #include <dali-toolkit/devel-api/builder/tree-node.h>
24
25 namespace Dali
26 {
27
28 namespace Toolkit
29 {
30
31 namespace Text
32 {
33
34 namespace
35 {
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( "" );
41
42 } // namespace
43
44 /**
45  * @brief Creates a map with pairs 'key,value' with the font's style parameters.
46  *
47  * @param[in] node Data structure with the font's style parameters.
48  * @param[out] map A map with the font's style parameters.
49  *
50  */
51 void CreateFontStyleMap( const TreeNode* const node, Property::Map& map )
52 {
53   switch( node->GetType() )
54   {
55     case TreeNode::IS_NULL:
56     case TreeNode::OBJECT:
57     case TreeNode::ARRAY: // FALL THROUGH
58     {
59       break;
60     }
61     case TreeNode::STRING:
62     {
63       map.Insert( node->GetName(), Property::Value( node->GetString() ) );
64       break;
65     }
66     case TreeNode::INTEGER:
67     case TreeNode::FLOAT:
68     case TreeNode::BOOLEAN: // FALL THROUGH
69     {
70       break;
71     }
72   }
73
74   for( TreeNode::ConstIterator it = node->CBegin(), endIt = node->CEnd(); it != endIt; ++it )
75   {
76     const TreeNode::KeyNodePair& pair = *it;
77     CreateFontStyleMap( &pair.second, map );
78   }
79 }
80
81 /**
82  * @brief Parses the font's style string.
83  *
84  * @param[in] style The font's style string.
85  * @param[out] map A map with the font's style parameters.
86  *
87  */
88 void ParseFontStyleString( const std::string& style, Property::Map& map )
89 {
90   Toolkit::JsonParser parser = Toolkit::JsonParser::New();
91
92   if( parser.Parse( style ) )
93   {
94     const TreeNode* const node = parser.GetRoot();
95
96     CreateFontStyleMap( node, map );
97   }
98 }
99
100 void SetFontStyleProperty( ControllerPtr controller, const Property::Value& value )
101 {
102   if( controller )
103   {
104     const std::string style = value.Get< std::string >();
105
106     // Stores the string to be recovered by the GetFontStyleProperty() function.
107     controller->SetDefaultFontStyle( style );
108
109     // Parses and applies the style.
110     Property::Map map;
111     ParseFontStyleString( style, map );
112
113     if( !map.Empty() )
114     {
115       /// Width key
116       Property::Value* widthValue = map.Find( WIDTH_KEY );
117
118       if( widthValue )
119       {
120         const std::string widthStr = widthValue->Get<std::string>();
121
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,
126                                                     width ) )
127         {
128           if( controller->GetDefaultFontWidth() != width )
129           {
130             controller->SetDefaultFontWidth( width );
131           }
132         }
133       }
134       else
135       {
136         controller->SetDefaultFontWidth( TextAbstraction::FontWidth::NORMAL );
137       }
138
139       /// Weight key
140       Property::Value* weightValue = map.Find( WEIGHT_KEY );
141
142       if( weightValue )
143       {
144         const std::string weightStr = weightValue->Get<std::string>();
145
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,
150                                                      weight ) )
151         {
152           if( controller->GetDefaultFontWeight() != weight )
153           {
154             controller->SetDefaultFontWeight( weight );
155           }
156         }
157       }
158       else
159       {
160         controller->SetDefaultFontWeight( TextAbstraction::FontWeight::NORMAL );
161       }
162
163       /// Slant key
164       Property::Value* slantValue = map.Find( SLANT_KEY );
165
166       if( slantValue )
167       {
168         const std::string slantStr = slantValue->Get<std::string>();
169
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,
174                                                     slant ) )
175         {
176           if( controller->GetDefaultFontSlant() != slant )
177           {
178             controller->SetDefaultFontSlant( slant );
179           }
180         }
181       }
182       else
183       {
184         controller->SetDefaultFontSlant( TextAbstraction::FontSlant::NORMAL );
185       }
186     }
187   }
188 }
189
190 void GetFontStyleProperty( ControllerPtr controller, Property::Value& value )
191 {
192   if( controller )
193   {
194     value = controller->GetDefaultFontStyle();
195   }
196 }
197
198 } // namespace Text
199
200 } // namespace Toolkit
201
202 } // namespace Dali