Markup procesor - Font.
[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     CreateFontStyleMap( node, map );
104   }
105 }
106
107 void SetFontStyleProperty( ControllerPtr controller, const Property::Value& value, FontStyle::Type type )
108 {
109   if( controller )
110   {
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() );
113
114     switch( type )
115     {
116       case FontStyle::DEFAULT:
117       {
118         // Stores the default font's style string to be recovered by the GetFontStyleProperty() function.
119         controller->SetDefaultFontStyle( style );
120         break;
121       }
122       case FontStyle::INPUT:
123       {
124         // Stores the input font's style string to be recovered by the GetFontStyleProperty() function.
125         controller->SetInputFontStyle( style );
126         break;
127       }
128     }
129
130     // Parses and applies the style.
131     Property::Map map;
132     ParseFontStyleString( style, map );
133
134     if( !map.Empty() )
135     {
136       /// Weight key
137       Property::Value* weightValue = map.Find( WEIGHT_KEY );
138
139       FontWeight weight = TextAbstraction::FontWeight::NORMAL;
140       const bool weightDefined = weightValue != NULL;
141       if( weightDefined )
142       {
143         const std::string weightStr = weightValue->Get<std::string>();
144
145         Scripting::GetEnumeration< FontWeight >( weightStr.c_str(),
146                                                  FONT_WEIGHT_STRING_TABLE,
147                                                  FONT_WEIGHT_STRING_TABLE_COUNT,
148                                                  weight );
149       }
150
151       /// Width key
152       Property::Value* widthValue = map.Find( WIDTH_KEY );
153
154       FontWidth width = TextAbstraction::FontWidth::NORMAL;
155       const bool widthDefined = widthValue != NULL;
156       if( widthDefined )
157       {
158         const std::string widthStr = widthValue->Get<std::string>();
159
160         Scripting::GetEnumeration< FontWidth >( widthStr.c_str(),
161                                                 FONT_WIDTH_STRING_TABLE,
162                                                 FONT_WIDTH_STRING_TABLE_COUNT,
163                                                 width );
164       }
165
166       /// Slant key
167       Property::Value* slantValue = map.Find( SLANT_KEY );
168
169       FontSlant slant = TextAbstraction::FontSlant::NORMAL;
170       const bool slantDefined = slantValue != NULL;
171       if( slantDefined )
172       {
173         const std::string slantStr = slantValue->Get<std::string>();
174
175         Scripting::GetEnumeration< FontSlant >( slantStr.c_str(),
176                                                 FONT_SLANT_STRING_TABLE,
177                                                 FONT_SLANT_STRING_TABLE_COUNT,
178                                                 slant );
179       }
180
181       switch( type )
182       {
183         case FontStyle::DEFAULT:
184         {
185           // Sets the default font's style values.
186           if( weightDefined && ( controller->GetDefaultFontWeight() != weight ) )
187           {
188             controller->SetDefaultFontWeight( weight );
189           }
190
191           if( widthDefined && ( controller->GetDefaultFontWidth() != width ) )
192           {
193             controller->SetDefaultFontWidth( width );
194           }
195
196           if( slantDefined && ( controller->GetDefaultFontSlant() != slant ) )
197           {
198             controller->SetDefaultFontSlant( slant );
199           }
200           break;
201         }
202         case FontStyle::INPUT:
203         {
204           // Sets the input font's style values.
205           if( weightDefined && ( controller->GetInputFontWeight() != weight ) )
206           {
207             controller->SetInputFontWeight( weight );
208           }
209
210           if( widthDefined && ( controller->GetInputFontWidth() != width ) )
211           {
212             controller->SetInputFontWidth( width );
213           }
214
215           if( slantDefined && ( controller->GetInputFontSlant() != slant ) )
216           {
217             controller->SetInputFontSlant( slant );
218           }
219           break;
220         }
221       }
222     }
223   }
224 }
225
226 void GetFontStyleProperty( ControllerPtr controller, Property::Value& value, FontStyle::Type type )
227 {
228   if( controller )
229   {
230     switch( type )
231     {
232       case FontStyle::DEFAULT:
233       {
234         value = controller->GetDefaultFontStyle();
235         break;
236       }
237       case FontStyle::INPUT:
238       {
239         value = controller->GetInputFontStyle();
240         break;
241       }
242     }
243   }
244 }
245
246 FontWeight StringToWeight( const char* const weightStr )
247 {
248   FontWeight weight = TextAbstraction::FontWeight::NORMAL;
249   Scripting::GetEnumeration<FontWeight>( weightStr,
250                                          FONT_WEIGHT_STRING_TABLE,
251                                          FONT_WEIGHT_STRING_TABLE_COUNT,
252                                          weight );
253
254   return weight;
255 }
256
257 FontWidth StringToWidth( const char* const widthStr )
258 {
259   FontWidth width = TextAbstraction::FontWidth::NORMAL;
260   Scripting::GetEnumeration<FontWidth>( widthStr,
261                                         FONT_WIDTH_STRING_TABLE,
262                                         FONT_WIDTH_STRING_TABLE_COUNT,
263                                         width );
264
265   return width;
266 }
267
268 FontSlant StringToSlant( const char* const slantStr )
269 {
270   FontSlant slant = TextAbstraction::FontSlant::NORMAL;
271   Scripting::GetEnumeration<FontSlant>( slantStr,
272                                         FONT_SLANT_STRING_TABLE,
273                                         FONT_SLANT_STRING_TABLE_COUNT,
274                                         slant );
275
276   return slant;
277 }
278
279 } // namespace Text
280
281 } // namespace Toolkit
282
283 } // namespace Dali