Merge "DALi C# binding - Avoid removal of dali-swig/manual/cpp/keyboard_focus_manager...
[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/internal/text/property-string-parser.h>
26 #include <dali-toolkit/internal/text/markup-processor-helper-functions.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 FAMILY_KEY( "family" );
44 const std::string TYPE_KEY( "type" );
45
46 const std::string SYSTEM_TOKEN( "system" );
47
48 #if defined(DEBUG_ENABLED)
49 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_CONTROLS");
50 #endif
51
52 } // namespace
53
54 void SetFontFamilyProperty( ControllerPtr controller, const Property::Value& value )
55 {
56   if( controller )
57   {
58     const std::string fontFamilyValue = value.Get<std::string>();
59
60     if( fontFamilyValue.empty() )
61     {
62       // Resets the default's font family name.
63       controller->SetDefaultFontFamily( "" );
64       return;
65     }
66
67     Property::Map map;
68     ParsePropertyString( fontFamilyValue, map );
69
70     if( map.Empty() )
71     {
72       // There is no map. The font has been passed as a font's family name with no format.
73       controller->SetDefaultFontFamily( fontFamilyValue );
74     }
75     else
76     {
77       /// Family key
78       Property::Value* familyValue = map.Find( FAMILY_KEY );
79
80       std::string fontFamilyName;
81       if( NULL != familyValue )
82       {
83         fontFamilyName = familyValue->Get<std::string>();
84       }
85
86       /// Type key
87       Property::Value* typeValue = map.Find( TYPE_KEY );
88
89       std::string typeStr;
90       if( NULL != typeValue )
91       {
92         typeStr = typeValue->Get<std::string>();
93       }
94
95       if( TokenComparison( SYSTEM_TOKEN, typeStr.c_str(), typeStr.size() ) )
96       {
97         controller->UpdateAfterFontChange( fontFamilyName );
98       }
99       else
100       {
101         controller->SetDefaultFontFamily( fontFamilyName );
102       }
103     }
104   }
105 }
106
107 void SetFontStyleProperty( ControllerPtr controller, const Property::Value& value, FontStyle::Type type )
108 {
109   if( controller )
110   {
111     Property::Map map;
112     if( Property::STRING == value.GetType() )
113     {
114       const std::string& fontStyleProperties = value.Get<std::string>();
115
116       ParsePropertyString( fontStyleProperties, map );
117     }
118     else
119     {
120       map = value.Get<Property::Map>();
121     }
122
123     if( !map.Empty() )
124     {
125       /// Weight key
126       Property::Value* weightValue = map.Find( WEIGHT_KEY );
127
128       FontWeight weight = TextAbstraction::FontWeight::NONE;
129       const bool weightDefined = weightValue != NULL;
130       if( weightDefined )
131       {
132         const std::string weightStr = weightValue->Get<std::string>();
133
134         Scripting::GetEnumeration< FontWeight >( weightStr.c_str(),
135                                                  FONT_WEIGHT_STRING_TABLE,
136                                                  FONT_WEIGHT_STRING_TABLE_COUNT,
137                                                  weight );
138       }
139
140       /// Width key
141       Property::Value* widthValue = map.Find( WIDTH_KEY );
142
143       FontWidth width = TextAbstraction::FontWidth::NONE;
144       const bool widthDefined = widthValue != NULL;
145       if( widthDefined )
146       {
147         const std::string widthStr = widthValue->Get<std::string>();
148
149         Scripting::GetEnumeration< FontWidth >( widthStr.c_str(),
150                                                 FONT_WIDTH_STRING_TABLE,
151                                                 FONT_WIDTH_STRING_TABLE_COUNT,
152                                                 width );
153       }
154
155       /// Slant key
156       Property::Value* slantValue = map.Find( SLANT_KEY );
157
158       FontSlant slant = TextAbstraction::FontSlant::NONE;
159       const bool slantDefined = slantValue != NULL;
160       if( slantDefined )
161       {
162         const std::string slantStr = slantValue->Get<std::string>();
163
164         Scripting::GetEnumeration< FontSlant >( slantStr.c_str(),
165                                                 FONT_SLANT_STRING_TABLE,
166                                                 FONT_SLANT_STRING_TABLE_COUNT,
167                                                 slant );
168       }
169
170       switch( type )
171       {
172         case FontStyle::DEFAULT:
173         {
174           // Sets the default font's style values.
175           if( !weightDefined ||
176               ( weightDefined && ( controller->GetDefaultFontWeight() != weight ) ) )
177           {
178             controller->SetDefaultFontWeight( weight );
179           }
180
181           if( !widthDefined ||
182               ( widthDefined && ( controller->GetDefaultFontWidth() != width ) ) )
183           {
184             controller->SetDefaultFontWidth( width );
185           }
186
187           if( !slantDefined ||
188               ( slantDefined && ( controller->GetDefaultFontSlant() != slant ) ) )
189           {
190             controller->SetDefaultFontSlant( slant );
191           }
192           break;
193         }
194         case FontStyle::INPUT:
195         {
196           // Sets the input font's style values.
197           if( !weightDefined ||
198               ( weightDefined && ( controller->GetInputFontWeight() != weight ) ) )
199           {
200             controller->SetInputFontWeight( weight );
201           }
202
203           if( !widthDefined ||
204               ( widthDefined && ( controller->GetInputFontWidth() != width ) ) )
205           {
206             controller->SetInputFontWidth( width );
207           }
208
209           if( !slantDefined ||
210               ( slantDefined && ( controller->GetInputFontSlant() != slant ) ) )
211           {
212             controller->SetInputFontSlant( slant );
213           }
214           break;
215         }
216       } // switch
217     } // map not empty
218     else
219     {
220       switch( type )
221       {
222         case FontStyle::DEFAULT:
223         {
224           controller->SetDefaultFontWeight( TextAbstraction::FontWeight::NONE );
225           controller->SetDefaultFontWidth( TextAbstraction::FontWidth::NONE );
226           controller->SetDefaultFontSlant( TextAbstraction::FontSlant::NONE );
227           break;
228         }
229         case FontStyle::INPUT:
230         {
231           controller->SetInputFontWeight( TextAbstraction::FontWeight::NONE );
232           controller->SetInputFontWidth( TextAbstraction::FontWidth::NONE );
233           controller->SetInputFontSlant( TextAbstraction::FontSlant::NONE );
234           break;
235         }
236       } // switch
237     } // map.Empty()
238   } // controller
239 }
240
241 void GetFontStyleProperty( ControllerPtr controller, Property::Value& value, FontStyle::Type type )
242 {
243   if( controller )
244   {
245     const bool isDefaultStyle = FontStyle::DEFAULT == type;
246
247     bool weightDefined = false;
248     bool widthDefined = false;
249     bool slantDefined = false;
250     FontWeight weight = TextAbstraction::FontWeight::NONE;
251     FontWidth width = TextAbstraction::FontWidth::NONE;
252     FontSlant slant = TextAbstraction::FontSlant::NONE;
253
254     if( isDefaultStyle )
255     {
256       weightDefined = controller->IsDefaultFontWeightDefined();
257       widthDefined = controller->IsDefaultFontWidthDefined();
258       slantDefined = controller->IsDefaultFontSlantDefined();
259
260       if( weightDefined )
261       {
262         weight = controller->GetDefaultFontWeight();
263       }
264
265       if( widthDefined )
266       {
267         width = controller->GetDefaultFontWidth();
268       }
269
270       if( slantDefined )
271       {
272         slant = controller->GetDefaultFontSlant();
273       }
274     }
275     else
276     {
277       weightDefined = controller->IsInputFontWeightDefined();
278       widthDefined = controller->IsInputFontWidthDefined();
279       slantDefined = controller->IsInputFontSlantDefined();
280
281       if( weightDefined )
282       {
283         weight = controller->GetInputFontWeight();
284       }
285
286       if( widthDefined )
287       {
288         width = controller->GetInputFontWidth();
289       }
290
291       if( slantDefined )
292       {
293         slant = controller->GetInputFontSlant();
294       }
295     }
296
297     Property::Map map;
298
299     if( weightDefined )
300     {
301       if( TextAbstraction::FontWeight::NONE != weight )
302       {
303         const std::string weightStr( GetEnumerationName( weight,
304                                                          FONT_WEIGHT_STRING_TABLE,
305                                                          FONT_WEIGHT_STRING_TABLE_COUNT ) );
306
307         map.Insert( WEIGHT_KEY, weightStr );
308       }
309     }
310
311     if( widthDefined )
312     {
313       if( TextAbstraction::FontWidth::NONE != width )
314       {
315         const std::string widthStr( GetEnumerationName( width,
316                                                         FONT_WIDTH_STRING_TABLE,
317                                                         FONT_WIDTH_STRING_TABLE_COUNT ) );
318
319         map.Insert( WIDTH_KEY, widthStr );
320       }
321     }
322
323     if( slantDefined )
324     {
325       if( TextAbstraction::FontSlant::NONE != slant )
326       {
327         const std::string slantStr( GetEnumerationName( slant,
328                                                         FONT_SLANT_STRING_TABLE,
329                                                         FONT_SLANT_STRING_TABLE_COUNT ) );
330
331         map.Insert( SLANT_KEY, slantStr );
332       }
333     }
334
335     value = map;
336   }
337 }
338
339 FontWeight StringToWeight( const char* const weightStr )
340 {
341   FontWeight weight = TextAbstraction::FontWeight::NORMAL;
342   Scripting::GetEnumeration<FontWeight>( weightStr,
343                                          FONT_WEIGHT_STRING_TABLE,
344                                          FONT_WEIGHT_STRING_TABLE_COUNT,
345                                          weight );
346
347   return weight;
348 }
349
350 FontWidth StringToWidth( const char* const widthStr )
351 {
352   FontWidth width = TextAbstraction::FontWidth::NORMAL;
353   Scripting::GetEnumeration<FontWidth>( widthStr,
354                                         FONT_WIDTH_STRING_TABLE,
355                                         FONT_WIDTH_STRING_TABLE_COUNT,
356                                         width );
357
358   return width;
359 }
360
361 FontSlant StringToSlant( const char* const slantStr )
362 {
363   FontSlant slant = TextAbstraction::FontSlant::NORMAL;
364   Scripting::GetEnumeration<FontSlant>( slantStr,
365                                         FONT_SLANT_STRING_TABLE,
366                                         FONT_SLANT_STRING_TABLE_COUNT,
367                                         slant );
368
369   return slant;
370 }
371
372 } // namespace Text
373
374 } // namespace Toolkit
375
376 } // namespace Dali