Remove the unused-variable gLogFilter in internal/text/text-font-style.cpp
[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 } // namespace
49
50 void SetFontFamilyProperty( ControllerPtr controller, const Property::Value& value )
51 {
52   if( controller )
53   {
54     const std::string fontFamilyValue = value.Get<std::string>();
55
56     if( fontFamilyValue.empty() )
57     {
58       // Resets the default's font family name.
59       controller->SetDefaultFontFamily( "" );
60       return;
61     }
62
63     Property::Map map;
64     ParsePropertyString( fontFamilyValue, map );
65
66     if( map.Empty() )
67     {
68       // There is no map. The font has been passed as a font's family name with no format.
69       controller->SetDefaultFontFamily( fontFamilyValue );
70     }
71     else
72     {
73       /// Family key
74       Property::Value* familyValue = map.Find( FAMILY_KEY );
75
76       std::string fontFamilyName;
77       if( NULL != familyValue )
78       {
79         fontFamilyName = familyValue->Get<std::string>();
80       }
81
82       /// Type key
83       Property::Value* typeValue = map.Find( TYPE_KEY );
84
85       std::string typeStr;
86       if( NULL != typeValue )
87       {
88         typeStr = typeValue->Get<std::string>();
89       }
90
91       if( TokenComparison( SYSTEM_TOKEN, typeStr.c_str(), typeStr.size() ) )
92       {
93         controller->UpdateAfterFontChange( fontFamilyName );
94       }
95       else
96       {
97         controller->SetDefaultFontFamily( fontFamilyName );
98       }
99     }
100   }
101 }
102
103 void SetFontStyleProperty( ControllerPtr controller, const Property::Value& value, FontStyle::Type type )
104 {
105   if( controller )
106   {
107     Property::Map map;
108     if( Property::STRING == value.GetType() )
109     {
110       const std::string& fontStyleProperties = value.Get<std::string>();
111
112       ParsePropertyString( fontStyleProperties, map );
113       controller->FontStyleSetByString( true );
114
115     }
116     else
117     {
118       map = value.Get<Property::Map>();
119       controller->FontStyleSetByString( false );
120     }
121
122     if( !map.Empty() )
123     {
124       /// Weight key
125       Property::Value* weightValue = map.Find( WEIGHT_KEY );
126
127       FontWeight weight = TextAbstraction::FontWeight::NONE;
128       const bool weightDefined = weightValue != NULL;
129       if( weightDefined )
130       {
131         const std::string weightStr = weightValue->Get<std::string>();
132
133         Scripting::GetEnumeration< FontWeight >( weightStr.c_str(),
134                                                  FONT_WEIGHT_STRING_TABLE,
135                                                  FONT_WEIGHT_STRING_TABLE_COUNT,
136                                                  weight );
137       }
138
139       /// Width key
140       Property::Value* widthValue = map.Find( WIDTH_KEY );
141
142       FontWidth width = TextAbstraction::FontWidth::NONE;
143       const bool widthDefined = widthValue != NULL;
144       if( widthDefined )
145       {
146         const std::string widthStr = widthValue->Get<std::string>();
147
148         Scripting::GetEnumeration< FontWidth >( widthStr.c_str(),
149                                                 FONT_WIDTH_STRING_TABLE,
150                                                 FONT_WIDTH_STRING_TABLE_COUNT,
151                                                 width );
152       }
153
154       /// Slant key
155       Property::Value* slantValue = map.Find( SLANT_KEY );
156
157       FontSlant slant = TextAbstraction::FontSlant::NONE;
158       const bool slantDefined = slantValue != NULL;
159       if( slantDefined )
160       {
161         const std::string slantStr = slantValue->Get<std::string>();
162
163         Scripting::GetEnumeration< FontSlant >( slantStr.c_str(),
164                                                 FONT_SLANT_STRING_TABLE,
165                                                 FONT_SLANT_STRING_TABLE_COUNT,
166                                                 slant );
167       }
168
169       switch( type )
170       {
171         case FontStyle::DEFAULT:
172         {
173           // Sets the default font's style values.
174           if( !weightDefined ||
175               ( weightDefined && ( controller->GetDefaultFontWeight() != weight ) ) )
176           {
177             controller->SetDefaultFontWeight( weight );
178           }
179
180           if( !widthDefined ||
181               ( widthDefined && ( controller->GetDefaultFontWidth() != width ) ) )
182           {
183             controller->SetDefaultFontWidth( width );
184           }
185
186           if( !slantDefined ||
187               ( slantDefined && ( controller->GetDefaultFontSlant() != slant ) ) )
188           {
189             controller->SetDefaultFontSlant( slant );
190           }
191           break;
192         }
193         case FontStyle::INPUT:
194         {
195           // Sets the input font's style values.
196           if( !weightDefined ||
197               ( weightDefined && ( controller->GetInputFontWeight() != weight ) ) )
198           {
199             controller->SetInputFontWeight( weight );
200           }
201
202           if( !widthDefined ||
203               ( widthDefined && ( controller->GetInputFontWidth() != width ) ) )
204           {
205             controller->SetInputFontWidth( width );
206           }
207
208           if( !slantDefined ||
209               ( slantDefined && ( controller->GetInputFontSlant() != slant ) ) )
210           {
211             controller->SetInputFontSlant( slant );
212           }
213           break;
214         }
215         case FontStyle::PLACEHOLDER:
216         {
217           // Sets the placeholder text font's style values.
218           if( !weightDefined ||
219               ( weightDefined && ( controller->GetPlaceholderTextFontWeight() != weight ) ) )
220           {
221             controller->SetPlaceholderTextFontWeight( weight );
222           }
223
224           if( !widthDefined ||
225               ( widthDefined && ( controller->GetPlaceholderTextFontWidth() != width ) ) )
226           {
227             controller->SetPlaceholderTextFontWidth( width );
228           }
229
230           if( !slantDefined ||
231               ( slantDefined && ( controller->GetPlaceholderTextFontSlant() != slant ) ) )
232           {
233             controller->SetPlaceholderTextFontSlant( slant );
234           }
235           break;
236         }
237       } // switch
238     } // map not empty
239     else
240     {
241       switch( type )
242       {
243         case FontStyle::DEFAULT:
244         {
245           controller->SetDefaultFontWeight( TextAbstraction::FontWeight::NONE );
246           controller->SetDefaultFontWidth( TextAbstraction::FontWidth::NONE );
247           controller->SetDefaultFontSlant( TextAbstraction::FontSlant::NONE );
248           break;
249         }
250         case FontStyle::INPUT:
251         {
252           controller->SetInputFontWeight( TextAbstraction::FontWeight::NONE );
253           controller->SetInputFontWidth( TextAbstraction::FontWidth::NONE );
254           controller->SetInputFontSlant( TextAbstraction::FontSlant::NONE );
255           break;
256         }
257         case FontStyle::PLACEHOLDER:
258         {
259           controller->SetPlaceholderTextFontWeight( TextAbstraction::FontWeight::NONE );
260           controller->SetPlaceholderTextFontWidth( TextAbstraction::FontWidth::NONE );
261           controller->SetPlaceholderTextFontSlant( TextAbstraction::FontSlant::NONE );
262           break;
263         }
264       } // switch
265     } // map.Empty()
266   } // controller
267 }
268
269 void GetFontStyleProperty( ControllerPtr controller, Property::Value& value, FontStyle::Type type )
270 {
271   if( controller )
272   {
273     const bool isSetbyString = controller->IsFontStyleSetByString();
274
275     bool weightDefined = false;
276     bool widthDefined = false;
277     bool slantDefined = false;
278     FontWeight weight = TextAbstraction::FontWeight::NONE;
279     FontWidth width = TextAbstraction::FontWidth::NONE;
280     FontSlant slant = TextAbstraction::FontSlant::NONE;
281
282     switch( type )
283     {
284       case FontStyle::DEFAULT:
285       {
286         weightDefined = controller->IsDefaultFontWeightDefined();
287         widthDefined = controller->IsDefaultFontWidthDefined();
288         slantDefined = controller->IsDefaultFontSlantDefined();
289
290         if( weightDefined )
291         {
292           weight = controller->GetDefaultFontWeight();
293         }
294
295         if( widthDefined )
296         {
297           width = controller->GetDefaultFontWidth();
298         }
299
300         if( slantDefined )
301         {
302           slant = controller->GetDefaultFontSlant();
303         }
304         break;
305       }
306       case FontStyle::INPUT:
307       {
308         weightDefined = controller->IsInputFontWeightDefined();
309         widthDefined = controller->IsInputFontWidthDefined();
310         slantDefined = controller->IsInputFontSlantDefined();
311
312         if( weightDefined )
313         {
314           weight = controller->GetInputFontWeight();
315         }
316
317         if( widthDefined )
318         {
319           width = controller->GetInputFontWidth();
320         }
321
322         if( slantDefined )
323         {
324           slant = controller->GetInputFontSlant();
325         }
326         break;
327       }
328       case FontStyle::PLACEHOLDER:
329       {
330         // The type is FontStyle::PLACEHOLDER
331         weightDefined = controller->IsPlaceholderTextFontWeightDefined();
332         widthDefined = controller->IsPlaceholderTextFontWidthDefined();
333         slantDefined = controller->IsPlaceholderTextFontSlantDefined();
334
335         if( weightDefined )
336         {
337           weight = controller->GetPlaceholderTextFontWeight();
338         }
339
340         if( widthDefined )
341         {
342           width = controller->GetPlaceholderTextFontWidth();
343         }
344
345         if( slantDefined )
346         {
347           slant = controller->GetPlaceholderTextFontSlant();
348         }
349         break;
350       }
351     }
352
353     if( !isSetbyString )
354     {
355       Property::Map map;
356
357       if( weightDefined )
358       {
359         if( TextAbstraction::FontWeight::NONE != weight )
360         {
361           const std::string weightStr( GetEnumerationName( weight,
362                                                           FONT_WEIGHT_STRING_TABLE,
363                                                           FONT_WEIGHT_STRING_TABLE_COUNT ) );
364
365           map.Insert( WEIGHT_KEY, weightStr );
366         }
367       }
368
369       if( widthDefined )
370       {
371         if( TextAbstraction::FontWidth::NONE != width )
372         {
373           const std::string widthStr( GetEnumerationName( width,
374                                                           FONT_WIDTH_STRING_TABLE,
375                                                           FONT_WIDTH_STRING_TABLE_COUNT ) );
376
377           map.Insert( WIDTH_KEY, widthStr );
378         }
379       }
380
381       if( slantDefined )
382       {
383         if( TextAbstraction::FontSlant::NONE != slant )
384         {
385           const std::string slantStr( GetEnumerationName( slant,
386                                                           FONT_SLANT_STRING_TABLE,
387                                                           FONT_SLANT_STRING_TABLE_COUNT ) );
388
389           map.Insert( SLANT_KEY, slantStr );
390         }
391       }
392
393       value = map;
394     } // SetbyMAP
395     else
396     {
397       std::string fontStyleProperties = "{";
398
399       if( weightDefined )
400       {
401         if( TextAbstraction::FontWeight::NONE != weight )
402         {
403           const std::string weightStr( GetEnumerationName( weight,
404                                                           FONT_WEIGHT_STRING_TABLE,
405                                                           FONT_WEIGHT_STRING_TABLE_COUNT ) );
406
407           fontStyleProperties += "\"weight\":\"" + weightStr + "\",";
408         }
409       }
410
411       if( widthDefined )
412       {
413         if( TextAbstraction::FontWidth::NONE != width )
414         {
415           const std::string widthStr( GetEnumerationName( width,
416                                                           FONT_WIDTH_STRING_TABLE,
417                                                           FONT_WIDTH_STRING_TABLE_COUNT ) );
418           fontStyleProperties += "\"width\":\"" + widthStr + "\",";
419         }
420       }
421
422       if( slantDefined )
423       {
424         if( TextAbstraction::FontSlant::NONE != slant )
425         {
426           const std::string slantStr( GetEnumerationName( slant,
427                                                           FONT_SLANT_STRING_TABLE,
428                                                           FONT_SLANT_STRING_TABLE_COUNT ) );
429
430           fontStyleProperties += "\"slant\":\"" + slantStr + "\"";
431         }
432       }
433
434       // If last character is comma, it will be removed.
435       if((*fontStyleProperties.rbegin()) == ',' )
436       {
437         fontStyleProperties = fontStyleProperties.substr( 0, fontStyleProperties.size()-1 );
438       }
439       fontStyleProperties += "}";
440
441       value = fontStyleProperties;
442     } // SetbyString
443   }// controller
444 }
445
446 FontWeight StringToWeight( const char* const weightStr )
447 {
448   FontWeight weight = TextAbstraction::FontWeight::NORMAL;
449   Scripting::GetEnumeration<FontWeight>( weightStr,
450                                          FONT_WEIGHT_STRING_TABLE,
451                                          FONT_WEIGHT_STRING_TABLE_COUNT,
452                                          weight );
453
454   return weight;
455 }
456
457 FontWidth StringToWidth( const char* const widthStr )
458 {
459   FontWidth width = TextAbstraction::FontWidth::NORMAL;
460   Scripting::GetEnumeration<FontWidth>( widthStr,
461                                         FONT_WIDTH_STRING_TABLE,
462                                         FONT_WIDTH_STRING_TABLE_COUNT,
463                                         width );
464
465   return width;
466 }
467
468 FontSlant StringToSlant( const char* const slantStr )
469 {
470   FontSlant slant = TextAbstraction::FontSlant::NORMAL;
471   Scripting::GetEnumeration<FontSlant>( slantStr,
472                                         FONT_SLANT_STRING_TABLE,
473                                         FONT_SLANT_STRING_TABLE_COUNT,
474                                         slant );
475
476   return slant;
477 }
478
479 } // namespace Text
480
481 } // namespace Toolkit
482
483 } // namespace Dali